【问题标题】:Python put request. Spotify API put request Malformed JsonPython 提出请求。 Spotify API 提出请求格式错误的 Json
【发布时间】:2020-07-24 09:50:48
【问题描述】:

我真的很感谢这里的一些帮助。我正在尝试使用 Spotify API 将专辑添加到用户库。我一直在与格式错误的 Json Payload 作斗争,完全没有想法。

这是我目前坐的简化版

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())

我收到的错误在 json 响应中:

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

我已尝试按以下方式更改 requests.put,但所有尝试都返回相同的错误

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))

【问题讨论】:

  • 嘿!你得到什么类型的错误?您还使用任何软件包来执行此操作吗?
  • 嘿,我收到一个 json 响应:{'error': {'status': 400, 'message': 'Malformed json payload'}} 我正在使用请求库
  • 你能import json 然后在你的响应变量集data = json.dumps(payload) 似乎另一个人有类似的问题,这里是链接:github.com/spotify/web-api/issues/502(可能是一个不同的用例但如果有帮助,请LMK)
  • 谢谢,但这仍然返回相同的错误。

标签: python api python-requests spotify


【解决方案1】:

07bYtmE3bPsLB6ZbmmFi8d:这个 spotify id 用于专辑 Dancefloor Hits #1。我检查了我的 Spotify 帐户,它在我帐户的相册中。下面是我运行它的代码。

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

【讨论】:

  • 酷没问题,我很高兴能帮上忙。只记得使用参数。如果您想看一下,实际上有一个名为 Spotipy 的 spotify 库。
猜你喜欢
  • 2016-04-28
  • 2018-06-07
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 2019-01-30
  • 1970-01-01
  • 2022-01-05
相关资源
最近更新 更多