繁体   English   中英

python 与 curl -d 命令的等效请求是什么?

[英]What is the equivalent request of python with curl -d command?

我很难找到与 curl 命令等效的 python 请求,该命令将正文中的数据作为 JSON 发送。

curl 命令看起来像

curl -X POST -d '{"key1": "value1", "key2": "value2"}' http://localhost:8080/myapi

通过 CURL 请求,Server(python) 获取 requset.form 中的数据,如下所示

ImmutableMultiDict([('{"key1": "value1", "key2": "value2"}', '')])

但是当像使用python的请求模块一样发送相同的post请求时

response = requests.post("http://localhost:8080/myapi", data=json.dumps({
    "key1": "value1", "key2": "value2"))

从服务器端,我看到的内容如下

ImmutableMultiDict([('key1', 'value1'), ('key2', 'value2')]) and its type is : <class 'werkzeug.datastructures.ImmutableMultiDict

我对为什么使用 requests.post 的请求与使用 curl 请求相比在服务器端具有不同的内容感到有些困惑,有人可以帮助理解和可能的解决方案


该问题已通过在 curl 和请求模块发出的请求中添加“Content-Type:application/json”解决。 另外,我曾经检查过 request.form 中的数据,但是当我使用 request.data 时意识到它正在工作,python 新手所以错过了这个,谢谢。

您是否尝试过只发送字典而不是字符串化的 json?

r = requests.post('https://httpbin.org/post', data = {"key1": "value1", "key2": "value2"})

通过 httpbin 测试服务器运行它并运行 curl 语句会产生相同的响应(用户代理除外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM