Http POST口音编码 [英] Http POST Accents encoding

查看:74
本文介绍了Http POST口音编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编码问题:当我在网络浏览器的输入中键入角色'é'时,它会发布为%E9,并且可以正常工作.另一方面,当我尝试使用Python和请求库发布请求时,它以%C3%A9的形式发送.

I have an encoding problem : When I type the caracter 'é' in an input in a web browser, it is posted as %E9, and it works fine. on the other hand, when I try to post a request using Python and requests library, it is sent as %C3%A9.

我该如何解决这个问题?

How could I solve the problem ?

这是无效的代码

requests.post("http://localhost", message = {"text":'é'})

谢谢

推荐答案

%C3%A9 是utf-8编码字符串的url编码版本:

%C3%A9 is url-encoded version of utf-8 encoded string:

>>> u'é'.encode('utf-8')
'\xc3\xa9'
>>> urllib.quote(u'é'.encode('utf-8'))
'%C3%A9'

使用 latin-1 编码(或类似方式)对字符串进行明确编码:

Explicitly encode the string with latin-1 encoding (or similar):

>>> u'é'.encode('latin1')
'\xe9'
>>> urllib.quote(u'é'.encode('latin-1'))
'%E9'


requests.post("http://localhost", message={"text": u'é'.encode('latin-1')})

这篇关于Http POST口音编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆