Http POST Accents encoding

久未见 提交于 2021-02-05 08:31:27

问题


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 ?

Here is the code that does not work

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

Thanks


回答1:


%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'

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')})


来源:https://stackoverflow.com/questions/25965362/http-post-accents-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!