4

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

2
  • You need to call params.encode('latin-1') Commented Jun 7, 2013 at 13:56
  • It doesn't work, same error... Other ideas ? I think I have to change the library that I use but I don't want to do that. Commented Jun 7, 2013 at 14:02

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@ kirelagin Can we use urlencode for odata related filters also? As we are constructing our get urls using odata filters. Thanks
@GauravParek I’m sorry, I have no idea what you are talking about. You should probably ask a separate question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.