python - How to send utf-8 content in a urllib2 request? -
i'm struggling following question past half day , although i've found info similar problems, nothing hits spot.
i'm trying send put request using urllib2 data contains unicode characters:
body = u'{ "bbb" : "asdf\xd7\xa9\xd7\x93\xd7\x92"}' conn = urllib2.request(request_url, body, headers) conn.get_method = lambda: 'put' response = urllib2.urlopen(conn)
i've tried use body = body.encode('utf-8')
, other variations, whatever following error:
unicodeencodeerror @ ... 'ascii' codec can't decode byte 0xc3 in position 15: ordinal not in range(128)
with 1 of following call stacks:
file "..." in ... 195. response = urllib2.urlopen(conn) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in urlopen 126. return _opener.open(url, data, timeout) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in open 394. response = self._open(req, data) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in _open 412. '_open', req) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in _call_chain 372. result = func(*args) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in http_open 1199. return self.do_open(httplib.httpconnection, req) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in do_open 1168. h.request(req.get_method(), req.get_selector(), req.data, headers) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py" in request 955. self._send_request(method, url, body, headers) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py" in _send_request 989. self.endheaders(body) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py" in endheaders 951. self._send_output(message_body) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py" in _send_output 815. self.send(message_body) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py" in send 787. self.sock.sendall(data) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/socket.py" in meth 224. return getattr(self._sock,name)(*args)
or following call stack (for when body = body.encode('utf-8')
):
file "..." in ... 195. response = urllib2.urlopen(conn) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in urlopen 126. return _opener.open(url, data, timeout) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in open 394. response = self._open(req, data) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in _open 412. '_open', req) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in _call_chain 372. result = func(*args) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in http_open 1199. return self.do_open(httplib.httpconnection, req) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/urllib2.py" in do_open 1168. h.request(req.get_method(), req.get_selector(), req.data, headers) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py" in request 955. self._send_request(method, url, body, headers) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py" in _send_request 989. self.endheaders(body) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py" in endheaders 951. self._send_output(message_body) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/httplib.py" in _send_output 809. msg += message_body
what doing wrong? how can send body unicode characters via urllib2? if there no unicode characters, works fine.
also note content-type
header set application/json;charset=utf-8
.
if it's relevant in way, context of i'm doing this: i'm getting request django server, , delegate request django server. don't redirect, send request own server response , send back. body
request.body
in django view.
edit:
my headers are:
{ 'origin': 'http://10.0.0.146:8000', 'accept-language': 'en-us,en;q=0.8', 'accept-encoding': 'gzip,deflate,sdch', 'host': 'localhost:5000', 'accept': 'application/json, text/plain, */*', 'user-agent': 'mozilla/5.0 (macintosh; intel mac os x 10_8_3) applewebkit/537.31 (khtml, gecko) chrome/26.0.1410.65 safari/537.31', 'accept-charset': 'iso-8859-1,utf-8;q=0.7,*;q=0.3', 'connection': 'keep-alive', 'x-requested-with': 'xmlhttprequest', 'pragma': 'no-cache', 'cache-control': 'no-cache', 'referer': 'http://localhost:5000/', 'content-type': 'application/json;charset=utf-8', 'authorization': 'apikey ogklpgsesnytogidbsldhjjvyvjcbg:0d5897b5204c2f2527f532c6a97ba18a7f06acdc', 'cookie': 'username=ogklpgsesnytogidbsldhjjvyvjcbg; _we_wk_ls_=%7b%22time%22%3a1369123506709%7d; __jwpusr=39e63770-ec5c-4b96-9f7f-b199703d0d36; sessionid=0d741a7560258b301979a1c853b42a81; api_key=0d5897b5204c2f2527f532c6a97ba18a7f06acdc' }
you need pass byte strings request
. applies headers, url , body.
if of 3 inputs contain unicode values, automatic conversions between unicode , strings take place when concatenating, invariably lead grief.
Comments
Post a Comment