将curlpost请求转换为python请求不起作用
这是我的旧代码,它作为命令工作(返回一个200)。现在我想使用请求为其编写一个python脚本。我正在尝试:将curlpost请求转换为python请求不起作用,python,json,python-requests,Python,Json,Python Requests,这是我的旧代码,它作为命令工作(返回一个200)。现在我想使用请求为其编写一个python脚本。我正在尝试: curl -i -H "Content-Type: application/json" -X POST -d '{"message": "test"}' http://localhost:5000/members 返回一个400。为什么?我怎样才能解决这个问题?顺便说一句,在该端口上运行的是一个flask rest api。您试图发送数据而没有实际组合数据。你只需要在{}的周围加上“
curl -i -H "Content-Type: application/json" -X POST -d '{"message":
"test"}' http://localhost:5000/members
返回一个400。为什么?我怎样才能解决这个问题?顺便说一句,在该端口上运行的是一个flask rest api。您试图发送数据而没有实际组合数据。你只需要在{}的周围加上“}”
header = {"Content-type": "application/json"}
r = requests.post("http://127.0.0.1:5000/members", data={"message": "test"}, headers=header)
尝试
json
而不是data
header = {"Content-type": "application/json"}
r = requests.post("http://127.0.0.1:5000/members", data='{"message": "test"}', headers=header)