Brendan
Brendan

Reputation: 300

Python put request. Spotify API put request Malformed Json

I would really appreciate some help here. I am trying to use the Spotify API to add albums to a users library. I have been struggling with a Malformed Json Payload, and am completely out of ideas.

Here is a simplified version of what I am currently sitting on

url = 'https://api.spotify.com/v1/me/albums'
payload = {'body': ['01kTgTBiZkCFY3ZH2hBH6u', '4sz6Fn4BYORRLIc1AvQwQx']}
headers = {'Authorization':'Bearer {}'.format(access_token), 'Content-Type':'application/json',}
response = requests.put(url,headers=headers, data=payload)
print(response.json())

The error I am receiving is in the json response:

{'error': {'status': 400, 'message': 'Malformed json payload'}}

I have tried changing requests.put as per below, but all attempts are returning the same error

response = requests.put(url,headers=headers, json=payload)
response = requests.put(url,headers=headers, data=json.dumps(payload))
response = requests.put(url,headers=headers, json=json.dumps(payload))

Upvotes: 1

Views: 1235

Answers (1)

Rohan Nagavardhan
Rohan Nagavardhan

Reputation: 442

07bYtmE3bPsLB6ZbmmFi8d: This spotify id is for the album, Dancefloor Hits #1. I checked my spotify acct and there it was in my Albums on my acct. Below is my code to run that.

import requests

url = "https://api.spotify.com/v1/me/albums"

payload = {"ids": "27cZdqrQiKt3IT00338dws"}
headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer %s' % (access_token) # paste your access token in here
}

''' you can use the same syntax as above but the key was that your ID of album had to be a **parameter** not ***data***. 
To do that you use the params kwarg'''

response = requests.request("PUT", url, headers=headers, params = payload)

print(response.status_code) # should print out 200 when you run the code
# shows whether status was valid

Upvotes: 1

Related Questions