python - 在python中发布错误请求rest api
问题描述
我正在尝试通过 Python 中的 REST API 发布一些数据。
数据.json
{
"LastModifiedAt": "2020-12-21T20:19:45.335Z",
...
...
}
我正在使用以下代码发布数据。
with open('data.json') as fh:
data = json.load(fh)
headers = {
'Content-Type': 'application/json',
'X-API-Key':'ABC=='
}
response = requests.post('https://myurl.net/api/v1/resource/int_key/endpoint', headers=headers,data=data)
我总是得到以下回应status_code = 400
{
"ModelState": {
"line": [
"Unexpected character encountered while parsing value: L. Path '', line 0, position 0."
]
},
"Message": "The request is invalid."
}
我该如何调试?根据 API 文档,我的 URL 是正确的。为什么它返回“错误请求”状态码?
解决方案
我替换data
了json
它并且它起作用了。
response = requests.post('https://myurl.net/api/v1/resource/int_key/endpoint', headers=headers,
json=data)
我按照 AndroidDev 的建议使用 Postman 来调试它。
推荐阅读
- python - 同时控制步进电机和相机
- bash - Bash 脚本自动回答命令
- javascript - 如何在 Hightcharts / Highstock 中获取隐藏系列的数据?
- arrays - 如何从 findOne Mongoose 获取对象内部的数组
- batch-file - 在睡眠事件上运行脚本
- r - nchar(desc) 中的错误:无效的多字节字符串,使用 map() 和 rvest 时网页抓取时的元素 1
- python - 如何删除数据类属性
- java - ListView 中的 Wicket 表单组件值更改会影响所有元素
- sql-server - 基于参数值的条件格式
- tomcat - 无法启动上下文路径 [/ords] 处带有 Tomcat 9.X 应用程序的 Oracle ORDS (19.x):java.nio.file.AccessDeniedException