我正在使用requests
对文件执行 HTTP PUT,但由于某种原因,它正在上传原始 ASCII 而不是首先对其进行 base64 编码。
files = {'file': ('mydata.csv', open('mydata.csv', 'rb'))}
...
try:
logging.info("Upload URL: " + insert_upload_url)
headers = {'Content-type': 'multipart/form-data'}
upload_res = requests.put(insert_upload_url, files=files,
data={'insertUpload': data}, headers=headers)
logging.info("Status: " + str(upload_res.status_code))
if upload_res.status_code != requests.codes.ok:
logging.info("Reason: " + upload_res.reason)
else:
logging.info("Response: " + upload_res.text)
except Exception as e:
logging.info("Error: " + e.message)
当我转储原始 HTTP 请求时,我看到数据没有被编码为application/octet-stream
base64,也不是 base64:
PUT /apibatchmember/services/rest/batchmemberservice/G9X7CsNn3HisxFdwAu4W76mBewUkQcKD_limGK1MDZ-eW3-olsn8HW0l5oePvEU_ZwHaPbEz2_c1YonjauDs7Jhk9DGvGNSTLMbgSSY9TGVuk00I_-tKPw8mjoXzC63YsFzBIYIeYXHKf34dYxmmhz4iSeDw/batchmember/insertUpload HTTP/1.1
Host: api.example.com
Content-Length: 5062
Content-type: multipart/form-data
Accept-Encoding: gzip, deflate, compress
Accept: */*
User-Agent: python-requests/1.2.3 CPython/2.7.5 Darwin/12.5.0
--5f934d42eabb4d7abe8bdef2cea94b6b
Content-Disposition: form-data; name="insertUpload"
<?xml version="1.0" encoding="UTF-8"?>
<insertUpload>
<criteria>LOWER(EMAIL)</criteria>
<fileName>email.csv</fileName>
<separator>,</separator>
<fileEncoding>UTF-8</fileEncoding>
<skipFirstLine>false</skipFirstLine>
<dateFormat>mm/dd/YYYY</dateFormat>
<mapping>
<column>
<colNum>0</colNum>
<fieldName>CUSTNUM</colNum>
<column>
<column>
<colNum>1</colNum>
<fieldName>FIRSTNAME</colNum>
<column>
<column>
<colNum>2</colNum>
<fieldName>LASTNAME</colNum>
<column>
<column>
<colNum>3</colNum>
<fieldName>EMAIL</colNum>
<column>
</mapping>
</insertUpload>
--5f934d42eabb4d7abe8bdef2cea94b6b
Content-Disposition: form-data; name="file"; filename="email.csv"
Content-Type: text/csv
\xef\xbb\xbf1045,Janice,Waddell,blah@example.com
1156,Scott,Sheldon,blah@example.com
1267,Adrianus,Lengkeek,blah@example.com
1295,EDWIN,ODIFE,blah@example.com
1345,Albert,Stephenson,blah@example.com
...
--5f934d42eabb4d7abe8bdef2cea94b6b--
如何使该text/csv
部分成为 base64 编码的流?
谢谢!
更新
我现在使用以下内容获得正确的附件标题:
with open('/Users/mark.richman/email.csv', 'rb') as fd:
b64data = base64.b64encode(fd.read())
files = {'file': ('email.csv', b64data, 'application/octet-stream')}
但是,我仍然没有进入Content-Transfer-Encoding: base64
文件附件标题。任何想法?
更新 2
我不得不像这样破解标题:files = {'file': ('email.csv', b64data, 'application/octet-stream\r\nContent-Transfer-Encoding: base64')}
我在 HTTP 转储中看到了标头,但我仍在返回HTTP 415 Unsupported Media Type
。
更新 3
看起来requests
需要一个 API 更新来支持我在这里尝试做的事情。multipart 的第一部分(XML 数据)需要Content-Type: text/xml
设置,API 目前不支持。讨论到这里。