首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python put请求。Spotify API put请求格式错误的Json

Python put请求。Spotify API put请求格式错误的Json
EN

Stack Overflow用户
提问于 2020-04-12 01:15:43
回答 1查看 575关注 0票数 1

我真的很感激你能帮上忙。我正在尝试使用Spotify API将专辑添加到用户库。我一直在为一个畸形的Json负载而苦苦挣扎,完全没有想法。

这是我目前所坐的简化版本

代码语言:javascript
复制
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响应中:

代码语言:javascript
复制
{'error': {'status': 400, 'message': 'Malformed json payload'}}

我已尝试按照下面的说明更改requests.put,但所有尝试都返回相同的错误

代码语言:javascript
复制
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))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-12 02:44:56

07bYtmE3bPsLB6ZbmmFi8d:这个spotify账号是为专辑“Dancefloor Hits #1”准备的。我检查了我的spotify账号,发现它就在我的账号的相册里。下面是我运行它的代码。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61160753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档