Dev'Dev
Dev'Dev

Reputation: 265

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

Upvotes: 4

Views: 4501

Answers (1)

kirelagin
kirelagin

Reputation: 13626

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.

Upvotes: 6

Related Questions