Python request post 参数不成功

2017-11-06 16:48:37 +08:00
 hagezhou
用 python 的 request 来 post url,参数总是传不进去

···
headers = {'content-type': 'application/json'}
payload = {'group_name': 'env', 'host_name': '192.168.xxx.xxx'}
r = requests.post('http://0.0.0.0:5000/xxx', data=json.dumps(payload), headers=headers)

print(r.text)
···

会返回如下:
{
"errors": [
"'group_name' is a required property",
"'host_name' is a required property"
],
"message": "Unprocessable Entity"
}

我用 postman 去 post 就是可以的,木有任何问题,这是为什么?

更新一下:
用 postman 看到的 header 如下:
Content-Length →66
Content-Type →text/plain; charset=utf-8
Date →Mon, 06 Nov 2017 08:56:04 GMT
Server →Werkzeug/0.12.2 Python/2.7.14

然后我将代码中的 headers = {'content-type': 'application/json'}改成 headers = {'Content-Type': 'text/plain'}
然后 print r.headers,结果是
{'Date': 'Mon, 06 Nov 2017 08:55:39 GMT',
'Content-Length': '159',
'Content-Type': 'application/json',
'Server': 'Werkzeug/0.12.2 Python/2.7.14'}
http://0.0.0.0:5000/tower/inventory

Content-Type 并没有生效,why
7821 次点击
所在节点    Python
14 条回复
Marmot
2017-11-06 16:56:39 +08:00
r = requests.post('http://0.0.0.0:5000/xxx', json=payload)
hagezhou
2017-11-06 16:59:13 +08:00
@Marmot 我根据 postman 的返回结果,把 headers = {'Content-Type': 'text/plain'} 改了,但是 print 出来的 header 中还是 json ?
Marmot
2017-11-06 17:00:33 +08:00
还有就是有可能有些 content-type 大小写敏感。
Marmot
2017-11-06 17:01:58 +08:00
@hagezhou 1 楼那个回复那种是 request 自己帮你处理了,你可以看一下源码。
gimp
2017-11-06 17:02:16 +08:00
r.headers 中保存的是服务器返回给你的 headers
focusheart
2017-11-06 17:04:04 +08:00
你的调用方法参数传递不太对,看返回情况,应该是不需要用 json.dumps 把字典对象转换为字符串吧?
参考 requests 官方的文档:
http://cn.python-requests.org/zh_CN/latest/user/quickstart.html#post

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

直接把字典对象给 data 参数,相当提交了一个这样的表单:

<form action="http://httpbin.org/post" method="post">
<input type="text" name="key1" value="value1"/>
<input type="text" name="key2" value="value2"/>
</form>

或者按照 1 楼的方式,会自动转 json 编码,看你的服务器端怎么要求输入格式的了。
hagezhou
2017-11-06 17:05:47 +08:00
@gimp python 这边返回的'Content-Type': 'application/json',这个和 postman 返回的 {'Content-Type': 'text/plain'} 还是不一致
Marmot
2017-11-06 17:06:37 +08:00
~/python3.5/site-packages/requests/api.py 看一下这个,你会明白很多。
hagezhou
2017-11-06 17:12:37 +08:00
@Marmot 翻了一下 api.py, 用 params=payload 好了,多谢
源码注释这么写的:
:param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param data: (optional) Dictionary or list of tuples ``[(key, value)]`` (will be form-encoded), bytes, or file-like object to send in the body of the :class:`Request`.
Marmot
2017-11-06 17:16:22 +08:00
@hagezhou 你这个不是发的 json 吧......
mec
2017-11-06 17:39:03 +08:00
调用的方式不对;
还有,你在 request header 里的 content-type 声明跟服务器返回给你的没联系吧。。。
dawncold
2017-11-06 21:31:34 +08:00
看起来像是服务端写得有问题,调试这种接口时可以用 https://requestb.in 这种工具
ry_wang
2017-11-06 21:53:55 +08:00
json= 改成 data=
Marsss
2017-11-07 08:42:45 +08:00
用 burp 代理,抓包看发出去的数据包,和正常是哪里不一样,就知道怎么解决问题了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/404003

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX