发送请求时出现 Content-Type 错误

时间:2021-04-10 00:18:54

标签: python python-requests twitch

我想通过 Twitch API 使用我自己的频道关注另一个频道。我根据 Develepor 页面中的代码在 python 中准备了代码。但是当我提出请求时,我收到一个错误,如 “请求正文无法解析。尝试的内容类型:“应用程序/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)

1 个答案:

答案 0 :(得分:0)

你告诉它你要发送 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)