UnicodeDecodeError when posting a Multipart-Encoded binary File #444

Closed
cedricbonhomme opened this Issue Feb 20, 2012 · 4 comments

Comments

Projects
None yet
4 participants
@cedricbonhomme

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.

@cedricbonhomme

This comment has been minimized.

Show comment
Hide comment
@cedricbonhomme

cedricbonhomme Feb 22, 2012

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'")...

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'")...

@kennethreitz

This comment has been minimized.

Show comment
Hide comment
@kennethreitz

kennethreitz Jun 15, 2012

Member

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

Member

kennethreitz commented Jun 15, 2012

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

@bryanhelmig

This comment has been minimized.

Show comment
Hide comment
@bryanhelmig

bryanhelmig Sep 27, 2012

Contributor

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).

Contributor

bryanhelmig commented Sep 27, 2012

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).

@phbcanada

This comment has been minimized.

Show comment
Hide comment
@phbcanada

phbcanada Feb 27, 2013

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)

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