Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character encoding in a GET request with http.client lib (Python)

I am a beginner in python and I coded this little script to send an HTTP GET request on my local server (localhost). It works great, except that I wish I could send Latin characters such as accents.

import http.client

httpMethod = "GET"
url = "localhost"
params = "Hello World"

def httpRequest(httpMethod, url, params):
    conn = http.client.HTTPConnection(url)
    conn.request(httpMethod, '/?param='+params)
    conn.getresponse().read()
    conn.close()
    return

httpRequest(httpMethod, url, params)

When I insert the words with accent in my parameter "params", this is the error message that appears:

UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 14: ordinal not in range(128)

I don't know if there is a solution using http.client library but I think so. When I look in the documentation http.client, I can see this:

HTTPConnection.request

Strings are encoded as ISO-8859-1, the default charset for HTTP

like image 817
Dev'Dev Avatar asked Oct 19 '25 16:10

Dev'Dev


1 Answers

You shouldn't construct arguments manually. Use urlencode instead:

>>> from urllib.parse import urlencode
>>> params = 'Aserejé'
>>> urlencode({'params': params})
'params=Aserej%C3%A9'

So, you can do:

conn.request(httpMethod, '/?' + urlencode({'params': params}))

Also note that yout string will be encoded as UTF-8 before being URL-escaped.

like image 113
kirelagin Avatar answered Oct 21 '25 05:10

kirelagin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!