Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests module, need to encode the body, why?

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.

like image 773
Roger Davies Avatar asked May 27 '16 11:05

Roger Davies


People also ask

What is the purpose of the request module in Python?

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).

Does Python request encode URL?

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.

How do you send data in a request body in Python?

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.)

What is the reason behind the requests library?

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.


1 Answers

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

like image 115
qvpham Avatar answered Oct 19 '22 07:10

qvpham