0

I am trying to update metadata for an existing playlist using web API's. In the code, below using: requests.put returns [400] as a response suggesting a bad request. I have also tried requests.post however, this returns[405] as the error code.

Can anyone spot if I have a syntax error? of is there some other issue I can't see?

note: I have had success retrieving data from the playlist using: get It just seems to be editing the details that is the issue.

Thanks for your help.

def update_metadata(access_token, playlist_id, playlist_name, playlist_description):

    playlist_endpoint = f"https://api.spotify.com/v1/playlists/{playlist_id}"
    metadata = {
            "name": 'playlist title', "description": 'TEST', "public": True
    }
    get_headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization": f"Bearer {access_token}"
    }

    res = requests.put(playlist_endpoint, headers=get_headers, data=metadata)
    print(res)
    return res 

Useful Links:

Error codes: https://developer.spotify.com/documentation/web-api/

Change playlist details: https://developer.spotify.com/console/put-playlist/

mark
  • 1
  • 1

2 Answers2

1

try to set

requests.put(..., json=metadata)

instead of

requests.put(..., data=metadata)

or try to convert data into JSON if you want to use data:

import json

metadata = json.dumps(metadata)
rzlvmp
  • 7,512
  • 5
  • 16
  • 45
  • Thanks for the recommendation. Switching to ```requests.put(..., json=metadata)``` Instead of ```requests.put(..., data=metadata)``` does the trick. – mark May 19 '22 at 13:35
0

I can't spot any syntax or logical error in your code.

I asume that Requests is building your request incorrectly. An Evidence for that is the status code 400 Bad Request you are recieving from the Spotify API.

Because of that I suggest to take a look at this answer.

Since v1.2.3 Requests added the PreparedRequest object. As per the documentation "it contains the exact bytes that will be sent to the server".

Following the instruction given in the answer, you can check how your request looks like inside and what exactly is send to the Spotify API. That way you can validate your request.

I would also recommend you to make the same request via a cmd call. That way you can check if the API endpoint is working correctly. For that you can use the example given in the Spotify API documentation. Simply edit the secrets with your details.

curl -X "PUT" "https://api.spotify.com/v1/playlists/" --data "{\"name\":\"Updated Playlist Name\",\"description\":\"Updated playlist description\",\"public\":false}" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer "

Note: Always share your progress, so we can keep up and might be able to help you, cheers! :)

Maik Hasler
  • 1,064
  • 6
  • 36
  • 1
    Thanks a lot for the very quick and detailed response. It appears using ```requests.put(..., json=metadata)``` instead of ```requests.put(..., data=metadata)``` as recommend by @rzlvmp does the trick – mark May 19 '22 at 13:32