我正在尝试使用 Unicode 字符串 (u'\xe4\xf6\xfc') 作为 Python 中的参数执行 HTTP POST,但我收到以下错误:
UnicodeEncodeError: 'ascii' 编解码器无法编码字符
这是用于制作 HTTP POST 的代码(使用 httplib2)
http = httplib2.Http()
userInfo = [('Name', u'\xe4\xf6\xfc')]
data = urlencode(userInfo)
resp, content = http.request(url, 'POST', body=data)
关于如何解决这个问题有什么想法吗?
最佳答案
您不能直接 POST Python Unicode 对象。您应该首先将其编码为 UTF-8 字符串:
name = u'\xe4\xf6\xfc'.encode('utf-8')
userInfo = [('Name', name)]
关于python - 统一码编码错误 : 'ascii' codec can't encode character when trying a HTTP POST in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3110104/