首页 > 解决方案 > 如何使用python发布请求,指定正文

问题描述

我正在尝试使用 python 发送 JSON 对象,但出现 422 错误。同时,使用邮递员,我可以通过 API 发布 JSON:首先我在 Authorization 和正文中输入身份验证,我指定 raw 并选择 JSON,然后我发布 JSON 对象并获得 200 响应。

在此处输入图像描述

但是用python:

endpoint = "some endpoint"
url = host + endpoint
headers={"Accept": "application/json",
         "Authorization": f"Bearer {bearer_token}"}
        
order = json.dumps(json_object, ensure_ascii=False)
send_data = requests.post(url, json=order, headers=headers)
print(send_data.json())
        
if send_data.status_code==200:
   print("Order successfully sent")
else:
   print(f"The following error was encountered. Error: {send_data.status_code}")

有什么问题?请指教

标签: pythonjsonpython-3.xpython-requestspostman

解决方案


尝试

headers={"Accept": "application/json",
         "Authorization": f"Bearer {bearer_token}"}

send_data = requests.post(url, data=order, headers=headers)

推荐阅读