gpt4 book ai didi

python - 统一码编码错误 : 'ascii' codec can't encode character when trying a HTTP POST in Python

转载 作者:太空狗 更新时间:2023-10-29 18:27:02 27 4
gpt4 key购买 nike

我正在尝试使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com