Python - Spotify API Returning Error 400 "Malformed JSON"

- 1 answer

Heyo. I'm trying to make a small application in my spare time that uses the Spotify API . I have managed to get my program to use oAuth 2 to let a user authorize my app to manipulate their Spotify, but I have run into a problem with a certain endpoint on the Spotify API.

The endpoint I am having trouble with is (here's a link to their docs for the endpoint Whenever I try to make a put request to the endpoint I receive a 400 status code with the message "Malformed json" I get this message even when I copy/paste their own json from the docs, so I don't think it's a problem with how I am formatting my json, besides I have used json before to call other endpoints and they haven't had a problem with my formatting on those calls.

Here is my code:

headers = {"Authorization":"Bearer {}".format(access_token)}

url = ''
payload = {"context_uri": "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr"}
r = requests.put(url, headers=headers, data=payload)

To clarify, access_token is the access token that I have gotten from their authorization process, and I am using python-requests to make the http requests (Here is the docs for that:

I am wondering if the problem is due to the fact that Spotify uses colons int their track IDs and colons are also used in JSON? I saw in another thread on here that I should try to add "Content-Type":"application/json" to my headers but that didn't change the outcome at all.

Any help is greatly appreciated, and if you need any more info please let me know. Thank you!



If your payload is a dict use json kwargs in requests lib. data works for string payload. Here you go:

r = requests.put(url, headers=headers, json=payload)