用 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