egt
egt

Reputation: 185

Http POST Accents encoding

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

Upvotes: 1

Views: 2366

Answers (1)

falsetru
falsetru

Reputation: 369164

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

Upvotes: 4

Related Questions