我试图模拟使用python所做的curl请求,调用是。请注意,
我使用的curl命令是
curl -k --dump-header - -H "Content-Type: application/json" -X POST --data '{"environment_name": "foo"}' https://localhost/api/v1/environment/
服务器的响应是成功的。
HTTP/1.1 201 CREATED
Date: Tue, 17 Jun 2014 00:59:59 GMT
Server: Server
Vary: Accept-Language,Cookie,User-Agent
Content-Language: en-us
Location: https://localhost/api/v1/environment/None/
Status: 201 CREATED
Content-Length: 0
Cneonction: close
Content-Type: text/html; charset=utf-8
但是,当我尝试在python中使用“请求”执行post请求时,我的脚本是
import json
data = {'enviornment_name' : 'foo'}
headers = {'Content-type' : 'application/json'}
response = requests.post("https://localhost/api/v1/environment", headers=headers, data=data, verify=False)
运行脚本时,我会得到一个巨大的堆栈跟踪,但红色部分是
E DecodeError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing: incorrect data check',))
我不知道为什么我的脚本不能通过python工作
发布于 2014-06-27 05:53:28
@Fabricator我需要verify=False,但是我注意到代码中有一件事是我正在使用的服务器的一个问题,我需要在URI的末尾拖尾'/‘。此外,我还需要json.dumps(数据)而不是json.dump(数据),以防其他人查看。谢谢你的帮助
发布于 2014-06-19 00:10:39
您的服务器声称返回的是gzip
'd内容,但它不是。响应是在头文件中返回Content-Encoding: gzip
。这可能是因为在请求中,默认情况下,它发送Accept-Encoding: gzip, compress
。除了评论中@Fabricator的建议之外,还应该尝试在headers字典中将"Accept-Encoding"
设置为None
。
标题字典如下所示:
headers = {'Content-Type': 'application/json', 'Accept-Encoding': None}
你对请求的调用看起来就像
requests.post(url, headers=headers, data=json.dumps(data), verify=False)
https://stackoverflow.com/questions/24254361
复制相似问题