发送请求时,我收到ContentType错误

2024-06-10 21:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过Twitch API使用我自己的频道关注另一个频道。我根据Develepor页面中的代码用python编写了代码。但是,当我发出请求时,会出现一个错误,如“请求正文不可解析”。尝试的内容类型为:“application/json\”。我的代码如下。我该怎么做?您能帮助我吗

import requests

headers = {
   'Content-Type':'application/json',
   'Client-Id': 'clientid',
   'Authorization': 'Bearer token',
}
data = {
    "to_id": "610766140", "from_id": "664978624"
}
response = requests.post("https://api.twitch.tv/helix/users/follows", headers=headers, data=data)
print(response.text)

Tags: 代码apiidjsondataapplicationresponse错误
1条回答
网友
1楼 · 发布于 2024-06-10 21:22:55

你告诉它你要发送JSON,然后你没有发送JSON

import requests

headers = {
   'Content-Type':'application/json',
   'Client-Id': 'clientid',
   'Authorization': 'Bearer token',
}
data = {
    "to_id": "610766140", "from_id": "664978624"
}
response = requests.post("https://api.twitch.tv/helix/users/follows", headers=headers, json=data)
print(response.text)

相关问题 更多 >