UnicodeEncodeError: 'ascii' codec can't encode character when trying a HTTP POST in Python
我正在尝试在python中使用unicode字符串(u'xe4xf6xfc')作为参数进行http发布,但收到以下错误:
unicodeencodeerror:"ascii"编解码器无法编码字符
这是用于生成HTTP POST的代码(使用httplib2)
1 2 3 4 5 | http = httplib2.Http() userInfo = [('Name', u'\xe4\xf6\xfc')] data = urlencode(userInfo) resp, content = http.request(url, 'POST', body=data) |
有什么解决办法吗?
不能直接发布python unicode对象。您应该首先将其编码为UTF-8字符串:
1 2 | name = u'\xe4\xf6\xfc'.encode('utf-8') userInfo = [('Name', name)] |