使用请求的带有base64编码附件的HTTP PUT

时间:2013-10-01 20:08:50

标签: python base64 python-requests

我正在使用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:

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更新来支持我在这里尝试做的事情。多部分的第一部分(XML数据)需要设置Content-Type: text/xml,并且API当前不支持此功能。讨论here

1 个答案:

答案 0 :(得分:3)

你必须做

import base64


response = requests.put(url, data={'insertUpload': base64.b64encode(data)}, files=files, ...)

您没有定义data,但如果您尝试使用文件对象,那么这将不起作用。

如果data是一个字符串,那么这将正常工作。


我最初误解了你的问题。

因此,如果您没有尝试避免将整个文件加载到内存中(看起来像是这样),那么您可以这样做

with open('mydata.csv', 'rb') as fd:
     b64data = base64.b64encode(fd.read())

files = {'file': ('mydata.csv', b64data, 'application/octet-stream')}
requests.put(...)  # everything here is the same as what you did

另一方面,如果您希望对数据进行编码而不将其完全加载到内存中,那么就没有现成的方法。要使整个内容正确地进行base64编码,它首先需要文件中的所有内容。考虑(例如)以下内容:

print(base64.b64encode('line\n'))
# => bGluZQo=
print(base64.b64encode('line\nline\n'))
# => bGluZQpsaW5lCg==

它们是相关的,但它们不能简单地连接起来。你可能有更好的运气,只需使用一个类似文件的对象,即可动态地对文本进行base64编码。祝你好运!