2

我正在使用 requests 模块做一个简单的 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

我的编码有问题。它没有使用application/octet-stream,因此编码不正确。从标题中,我看到:

{
  u'origin': u'xxxx, xxxxxx', 
  u'files': {
               u'sample': u'data:None;base64,qANQR1DBw..........

所以,当我尝试data:None使用. 文件大小和编码不正确。data:application/octet-streamcurl

如何强制或检查它是否正在使用application/octet-stream

4

1 回答 1

1

样本取自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)

您可能想headers

headers = {'content-type': 'application/octet-stream'}
response = requests.post( url, files=sample, params=params, verify=False,
             headers = headers)
于 2013-10-28T07:57:25.740 回答