[GH-ISSUE #194] Update Playlist Image Bad Request #130

Closed
opened 2026-02-27 19:26:13 +03:00 by kerem · 3 comments
Owner

Originally created by @MadalenaSousa on GitHub (May 17, 2020).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/194

Hi there!

I'm trying to make an image the cover of a playlist and I keep getting a Bad Request error.
The picture is base64 encoded, a jpeg, I've reduced it to 0.01 of it's original size and the permissions are the ones refered on the spotify api website.
Any ideia on what this error could be?

Here is the full error:
Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: Bad request. in ...\vendor\jwilsson\spotify-web-api-php\src\Request.php:45 Stack trace: #0 ..\vendor\jwilsson\spotify-web-api-php\src\Request.php(254): SpotifyWebAPI\Request->parseBody(Object(stdClass), 400) #1 ..\vendor\jwilsson\spotify-web-api-php\src\Request.php(126): SpotifyWebAPI\Request->send('PUT', 'https://api.spo...', 'ZGF0YTppbWFnZS9...', Array) #2..\vendor\jwilsson\spotify-web-api-php\src\SpotifyWebAPI.php(76): SpotifyWebAPI\Request->api('PUT', '/v1/playlists/1...', 'ZGF0YTppbWFnZS9...', Array) #3 ...\vendor\jwilsson\spotify-web-api-php\src\SpotifyWebAPI.php(1705): SpotifyWebAPI\SpotifyWebAPI->sendRequest('PUT', '/v1/playlists/1...', 'ZGF0YTppbWFnZS9...') #4 ..\php\createPlaylist.php(23): SpotifyWebAPI\SpotifyWebAPI->updatePlaylistImage('1UJzLOz61XQJV7Z...', 'ZGF0YTppbWFnZS9...') #5 {main} thrown in ..\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 45

Originally created by @MadalenaSousa on GitHub (May 17, 2020). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/194 Hi there! I'm trying to make an image the cover of a playlist and I keep getting a Bad Request error. The picture is base64 encoded, a jpeg, I've reduced it to 0.01 of it's original size and the permissions are the ones refered on the spotify api website. Any ideia on what this error could be? Here is the full error: Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: Bad request. in ...\vendor\jwilsson\spotify-web-api-php\src\Request.php:45 Stack trace: #0 ..\vendor\jwilsson\spotify-web-api-php\src\Request.php(254): SpotifyWebAPI\Request->parseBody(Object(stdClass), 400) #1 ..\vendor\jwilsson\spotify-web-api-php\src\Request.php(126): SpotifyWebAPI\Request->send('PUT', 'https://api.spo...', 'ZGF0YTppbWFnZS9...', Array) #2..\vendor\jwilsson\spotify-web-api-php\src\SpotifyWebAPI.php(76): SpotifyWebAPI\Request->api('PUT', '/v1/playlists/1...', 'ZGF0YTppbWFnZS9...', Array) #3 ...\vendor\jwilsson\spotify-web-api-php\src\SpotifyWebAPI.php(1705): SpotifyWebAPI\SpotifyWebAPI->sendRequest('PUT', '/v1/playlists/1...', 'ZGF0YTppbWFnZS9...') #4 ..\php\createPlaylist.php(23): SpotifyWebAPI\SpotifyWebAPI->updatePlaylistImage('1UJzLOz61XQJV7Z...', 'ZGF0YTppbWFnZS9...') #5 {main} thrown in ..\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 45
kerem 2026-02-27 19:26:13 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (May 17, 2020):

Hey!
That's strange. It's working when I try it.

Could you share the code you're using? And have you tried with another playlist and/or image?

<!-- gh-comment-id:629848291 --> @jwilsson commented on GitHub (May 17, 2020): Hey! That's strange. It's working when I try it. Could you share the code you're using? And have you tried with another playlist and/or image?
Author
Owner

@MadalenaSousa commented on GitHub (May 17, 2020):

Sure! I'll try share the important parts:

I have a form that runs this php code:
$playlistName = $_POST['playlistname']; //creates playlist
$newPlaylist = $api->createPlaylist([
'name' => $playlistName
]);

$songTotal = $_POST['songTotal']; //add songs
for($i = 0; $i < $songTotal; $i++) {
$api->addPlaylistTracks($newPlaylist->id, [
$_POST['songId' . $i]
]);
}

if($_POST['cover'] == 'use') { //define cover
$imageData = base64_encode($_POST['playlistImg']);
$api->updatePlaylistImage($newPlaylist->id, $imageData);
}

Then the image that I want to use is the canvas, converted to jpg:
let canvas = document.getElementById('defaultCanvas0');
let img = new Image(200, 200);
img.src = canvas.toDataURL('image/jpeg', 0.01); //this defines the format and the quality
document.querySelector('.preview').appendChild(img);

Since this all happens in a pop up, I'm creating the input with the image data:
let playlistImg = document.createElement('input');
playlistImg.setAttribute('type', 'hidden');
playlistImg.setAttribute('name', 'playlistImg');
playlistImg.setAttribute('value', img.src);

document.querySelector('.create-playlist form').appendChild(playlistImg);

I've checked if the base64 encoding is right, and it seems to be.
Thank you for the quick response

<!-- gh-comment-id:629852716 --> @MadalenaSousa commented on GitHub (May 17, 2020): Sure! I'll try share the important parts: **I have a form that runs this php code:** $playlistName = $_POST['playlistname']; //creates playlist $newPlaylist = $api->createPlaylist([ 'name' => $playlistName ]); $songTotal = $_POST['songTotal']; //add songs for($i = 0; $i < $songTotal; $i++) { $api->addPlaylistTracks($newPlaylist->id, [ $_POST['songId' . $i] ]); } if($_POST['cover'] == 'use') { //define cover $imageData = base64_encode($_POST['playlistImg']); $api->updatePlaylistImage($newPlaylist->id, $imageData); } **Then the image that I want to use is the canvas, converted to jpg:** let canvas = document.getElementById('defaultCanvas0'); let img = new Image(200, 200); img.src = canvas.toDataURL('image/jpeg', 0.01); //this defines the format and the quality document.querySelector('.preview').appendChild(img); **Since this all happens in a pop up, I'm creating the input with the image data:** let playlistImg = document.createElement('input'); playlistImg.setAttribute('type', 'hidden'); playlistImg.setAttribute('name', 'playlistImg'); playlistImg.setAttribute('value', img.src); document.querySelector('.create-playlist form').appendChild(playlistImg); I've checked if the base64 encoding is right, and it seems to be. Thank you for the quick response
Author
Owner

@MadalenaSousa commented on GitHub (May 17, 2020):

All right, I tryed with another image like you said and it works. It's my mistake, I'll figure it out, thank you for your help!

<!-- gh-comment-id:629855621 --> @MadalenaSousa commented on GitHub (May 17, 2020): All right, I tryed with another image like you said and it works. It's my mistake, I'll figure it out, thank you for your help!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#130
No description provided.