[英]Python 'requests' module and encoding?
我正在使用請求模塊執行一個簡單的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..........
所以,當我嘗試curl
時,我得到data:None
而不是data:application/octet-stream
。 文件大小和編碼不正確。
如何強制或檢查它是否正在使用application/octet-stream
?
樣本取自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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.