简体   繁体   中英

Python 'requests' module and encoding?

I am doing a simple POST request using the requests module, and testing it against 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. 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 . The file size and encoding is incorrect.

How can I force or check that it is using application/octet-stream ?

Sample taken from 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 = {'content-type': 'application/octet-stream'}
response = requests.post( url, files=sample, params=params, verify=False,
             headers = headers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM