I was struggling with sending a request to a web service using the requests module. I'm using Python 3.5.1-32 bit on Windows.
Whenever I request something that contains, for example, a latin accented character such as "á" then I need to encode my string variable explicitly as utf-8. Can someone explain why I need to do this?
Here is my code example:
import requests
headers = { "content-type" : "text/xml;charset=UTF-8" }
url = '....'
body = 'á'
requests.post(url, data=body, headers=headers)
This doesn't work, the server receives an escaped version of the character, but if I add this before the request:
body = body.encode(encoding='utf-8')
It works.
Can somebody explain why this is necessary? My Python-Fu is such that it took me a good while to figure this out.
The requests module allows you to send HTTP requests using Python. The HTTP request returns a Response Object with all the response data (content, encoding, status, etc).
In Python, we can URL encode a query string using the urlib. parse module, which further contains a function urlencode() for encoding the query string in URL. The query string is simply a string of key-value pairs.
You'll want to adapt the data you send in the body of your request to the specified URL. Syntax: requests. post(url, data={key: value}, json={key: value}, headers={key:value}, args) *(data, json, headers parameters are optional.)
Requests library can be used to scrape the data from the website. Using requests, you can get, post, delete, update the data for the URL given. The handling of cookies and session is very easy. The security is also taken care of the help of authentication module support.
requests doc says:
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
In Python 3 body='á'
is a unicode string. With the method encode()
you are converting it to bytes.
For a unicode string, requests understands it as file content and encodes with octet-stream/base64. The false content will be transferred.
P/S: It's only for Python 3. str
of Python 2 isn't like str
in Python 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With