python - 如何使用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}")
有什么问题?请指教
解决方案
尝试
headers={"Accept": "application/json",
"Authorization": f"Bearer {bearer_token}"}
send_data = requests.post(url, data=order, headers=headers)
推荐阅读
- javascript - JavaScript 功能仅在页面重新加载后有效
- html - 如何在 html 表格列中打印一天中的小时数?
- flutter - Flutter:将 Base64 String 图像 url 转换为 File 并在 FileImage 或相关小部件中使用
- python - Flutter 和 python [AES] 之间的相同加密
- javascript - MuleSoft 解析模板参考
- c++ - SDL2 不会为重音键生成 KEYDOWN 和 KEYUP 事件
- django - django jsonField 过滤以从指定键中获取值列表
- python - Spyder 中安装的软件包未显示在 Sublime 文本中(OS High Sierra)
- excel - 输入新数据时需要自动对 2 列进行排序
- android - 哪个是检查用户位置(android 应用程序)是否在多边形内的更好方法