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

问题描述

我想通过 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)

解决方法

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