Character encoding in a GET request with http.client lib (Python)
Question: 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


Answer: You shouldn't construct arguments manually. Use
[`urlencode`](http://docs.python.org/3/library/urllib.parse.html#urllib.parse.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_.

