python使用JSON数据发出POST请求 [英] python making POST request with JSON data
问题描述
我正在尝试模仿我正在使用python调用的curl请求.请注意
I'm trying to mimic a curl request I'm doing using python the call is. Note that
- HTTPS请求
- 忽略SSL认证验证
- 我的服务器是django
我使用的curl命令是
The curl command I used is
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
但是,当我尝试使用"requests"在python中进行发布请求时,我的脚本是
However when I try to do a post request in python with 'requests' my script is
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)
运行脚本时,我获得了巨大的堆栈跟踪,但是红色部分是
When running the script I get back a huge stack trace but the part in red is
E DecodeError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing: incorrect data check',))
我不确定为什么我的脚本无法通过python工作
I'm not sure why I can't my script to work via python
推荐答案
@Fabricator我需要verify = False,但是我注意到代码中有一件事是我正在使用的服务器的问题,我需要结尾的'/"(位于URI的末尾).另外,我还需要json.dumps(data)而不是json.dump(data),以防其他人正在寻找.感谢您的帮助
@Fabricator I need the verify=False however I noticed the one thing in my code that was an issue for the server I was using I needed the trailing '/' at the end of the URI. In addition I also needed the json.dumps(data) not json.dump(data) in case others are looking. Thanks for the help
这篇关于python使用JSON数据发出POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!