当我通过邮递员发送 PUT 请求时一切正常,但是当我将代码从邮递员复制到 python 时,我得到了这个:
{"code": 50035, "errors": {"_errors": [{"code": "CONTENT_TYPE_INVALID", "message": "Expected \"Content-Type\" header to be one of {'application/json'}."}]}, "message": "Invalid Form Body"}
代码:
url = "https://discord.com/api/v9/guilds/860859559908474900/premium/subscriptions"
payload={'user_premium_guild_subscription_slot_ids': '853228009795223572'}
files=[
]
headers = {
'authorization': 'here token',
'Cookie': '__dcfduid=c5cbabf10a8344558c8c4b90a6e306e2'
}
response = requests.request("PUT", url, headers=headers, data=payload, files=files)
print(response.text)
如果我添加 'content-type' : 'application/json' 到标题,我得到这个:
{"message": "400: Bad Request", "code": 0}
答案 0 :(得分:1)
要么使用 json=
而不是 data=
,要么添加 Content-Type
标头,其值为 application/json
,正如服务器告诉您的那样。
还要检查 Authorization
标头,主要是有一个前缀提到要使用的身份验证,例如:
Authorization: Basic base64-encoded(user:pass)
Authorization: Bearer token
Authorization: Token token
并确保 API 实际上允许 files
参数为空,如果不允许,请尝试将其删除。
然后是您提供的 __dcfduid
cookie。一些 cookie 往往是动态的,因为请求之间的值会发生变化。然后尝试建立会话并在其中执行登录部分:
import requests as r
ses = r.Session()
resp = ses.post(<login to the API>)
print(resp.text)
ses.put(
url="https://discord.com/api/v9/guilds/860859559908474900/premium/subscriptions",
headers={
'Authorization': 'here token',
# cookies are in the ses object
},
json={
'user_premium_guild_subscription_slot_ids': '853228009795223572'
},
# if required
files=[...]
)