[英]Python 'requests' module and encoding?
I am doing a simple POST request using the requests module, and testing it against httpbin 我正在使用请求模块执行一个简单的POST请求,并针对httpbin对其进行测试
import requests
url = 'http://httpbin.org/post'
params = {'apikey':'666666'}
sample = {'sample': open('test.bin', 'r')}
response = requests.post( url, files=sample, params=params, verify=False)
report_info = response.json()
print report_info
I have an issue with the encoding. 我的编码有问题。 It is not using application/octet-stream
and so the encoding is not correct. 它未使用application/octet-stream
,因此编码不正确。 From the headers, I see: 从标题中,我看到:
{
u'origin': u'xxxx, xxxxxx',
u'files': {
u'sample': u'data:None;base64,qANQR1DBw..........
So, I get data:None
instead of data:application/octet-stream
when I try with curl
. 所以,当我尝试curl
时,我得到data:None
而不是data:application/octet-stream
。 The file size and encoding is incorrect. 文件大小和编码不正确。
How can I force or check that it is using application/octet-stream
? 如何强制或检查它是否正在使用application/octet-stream
?
Sample taken from http://www.python-requests.org/en/latest/user/quickstart/#custom-headers 样本取自http://www.python-requests.org/en/latest/user/quickstart/#custom-headers
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> headers = {'content-type': 'application/json'}
>>> r = requests.post(url, data=json.dumps(payload), headers=headers)
You might want to change the headers
to 您可能需要将headers
更改为
headers = {'content-type': 'application/octet-stream'}
response = requests.post( url, files=sample, params=params, verify=False,
headers = headers)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.