Skip to content
This repository

UnicodeDecodeError when posting a Multipart-Encoded binary File #444

Closed
cedricbonhomme opened this Issue February 19, 2012 · 4 comments

4 participants

Cédric Bonhomme Kenneth Reitz Bryan Helmig Paul B
Cédric Bonhomme

I think that there is still a bug related to the bug #403.

>>> files = {"xml": open("photos.xml", "r"), "image": open("Vienna-Orchestra.jpg", "rb")}
>>> r = requests.post(url, files=files, auth=('***', '***'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-i686/egg/requests/api.py", line 84, in post
  File "build/bdist.linux-i686/egg/requests/api.py", line 39, in request
  File "build/bdist.linux-i686/egg/requests/sessions.py", line 200, in request
  File "build/bdist.linux-i686/egg/requests/models.py", line 535, in send
  File "build/bdist.linux-i686/egg/requests/packages/urllib3/connectionpool.py", line 370, in urlopen
  File "build/bdist.linux-i686/egg/requests/packages/urllib3/connectionpool.py", line 246, in _make_request
  File "/usr/local/lib/python2.7/httplib.py", line 946, in request
    self._send_request(method, url, body, headers)
  File "/usr/local/lib/python2.7/httplib.py", line 987, in _send_request
    self.endheaders(body)
  File "/usr/local/lib/python2.7/httplib.py", line 940, in endheaders
    self._send_output(message_body)
  File "/usr/local/lib/python2.7/httplib.py", line 801, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 484: ordinal not in range(128)

I've made different tests and always get the same problem. It works quite well if the photo is a simple text file.

Cédric Bonhomme

I'm trying to solve the problem in urllib3/connectionpool.py line 370 by this way:

# Make the request on the httplib connection object
            httplib_response = self._make_request(conn, method, url,
                                                  timeout=timeout,
                                                  body=unicode.encode(unicode(body, errors='ignore')), headers=headers)

The photo and XML file are now sent to the server without errors. But the photograph can't be displayed since it contains less information than the original (due to "errors='ignore'")...

Kenneth Reitz
Owner

It looks to me like this should be filed against shazow/urllib3

Kenneth Reitz kennethreitz closed this June 15, 2012
Bryan Helmig

If anyone runs across this issue, be sure that all the arguments you pass into requests are byte strings (besides just the payload or data). Any unicode instances making their way in will pollute the message building in httplib (in my case it was passing in a unicode u"POST" method).

Paul B

So I don't understand why I'm still getting this error? I'm not passing any unicode. Trying to post a binary file via:
response = requests.post(url, files=files, auth=oauth, params=params, verify=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.