Python: Http Put With Binary Data
Solution 1:
It's because
data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format.
from urllib2 doc
Solution 2:
You are trying to automatically convert a python unicode string to a regular byte string. JSoN is always unicode, but HTTP must send bytes. If you are confident that the reciever will understand the json encoded data in a particular encoding, you can just encode it that way:
>>> urllib2.urlopen(urllib2.Request("http://example.com", data=u'\u0ca0'))
Traceback (most recent call last):
...
UnicodeEncodeError: 'ascii' codec cannot encode character u'\u0ca0'in position 0: ordinal notinrange(128)
>>> urllib2.urlopen(urllib2.Request("http://example.com",
... data=u'\u0ca0'.encode('utf-8')))
<addinfourl at 15700984 whose fp = <socket._fileobject object at 0xdfbe50>>
>>>
Note the .encode('utf-8')
, which converts unicode
to str
in utf-8. The implicit conversion would use ascii, which cant encode non-ascii characters.
tl;dr ... data=json.dumps(blabla).encode('utf-8') ...
Solution 3:
According to the urllib2 documentation, you will need to percent-encode the byte-string.
Post a Comment for "Python: Http Put With Binary Data"