Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将curlpost请求转换为python请求不起作用_Python_Json_Python Requests - Fatal编程技术网

将curlpost请求转换为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。您试图发送数据而没有实际组合数据。你只需要在{}的周围加上“

这是我的旧代码,它作为命令工作(返回一个200)。现在我想使用请求为其编写一个python脚本。我正在尝试:

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)