0

为什么我会收到这样的回复?我试图在 Discord 上询问人们,但没有人能解释。

>>>import requests
>>>api_url = "my_url"
>>>headers = {"Content-type": "application/json"}
>>>res = requests.post(api_url, "text to post", headers=headers)
>>>res.json()
{'message': '400: Bad Request', 'code': 0}
4

2 回答 2

1

由于您已从服务器收到此响应:

{'message': '400: Bad Request', 'code': 0}

它表明您实际上已经到达了服务器端点,因此您可能只是没有包含服务器期望请求的所有数据。

请注意,文档指定“数据”应该是“(可选)字典、元组列表、字节或要在请求正文中发送的类似文件的对象”,因此可能是事先对文本进行编码的最佳实践,例如:

my_text = "some json encoded text"
requests.post(api_url, my_text.encode("ascii"), headers = headers)

仔细检查您发送的数据(尤其是拼写)是否与服务器对请求的要求相匹配。

于 2021-09-14T14:23:13.657 回答
-1

尝试这个:

import requests
import json

api_url = "my_url"
body = {'xxx': 'text to post'}
headers = {"Content-type": "application/json"}
res = requests.post(api_url, data=json.dumps(body), headers=headers)
res.json()
于 2021-09-14T14:18:06.290 回答