Streaming API tends to keep the request open allowing us to collect the stream data in real time. While dealing with a continuous stream of data, to ensure that none of the messages being missed from it we can take the help of iter_lines()
in Requests. The iter_lines()
iterates over the response data line by line. This can be achieved by setting the parameter stream as True
while sending the request.
Consider the following example taken from http://docs.python-requests.org/en/latest/user/advanced/#streaming-requests:
>>> import json >>> import requests >>> r = requests.get('http://httpbin.org/stream/4', stream=True) >>> for line in r.iter_lines(): ... if line: ... print(json.loads(line) )
In the preceding example, the response contains a stream of data. With the help of iter_lines()
, we tried to print the data by iterating through every line.
As specified in the HTTP protocol (RFC 7230), applications can request the server to return the HTTP responses in an encoded format. The process of encoding turns the response content into an understandable format which makes it easy to access it. When the HTTP header fails to return the type of encoding, Requests will try to assume the encoding with the help of chardet
.
If we access the response headers of a request, it does contain the keys of content-type
. Let us look at a response header's content-type
:
>>> re = requests.get('http://google.com') >>> re.headers['content-type'] 'text/html; charset=ISO-8859-1'
In the preceding example the content type contains 'text/html; charset=ISO-8859-1'
. This happens when the Requests finds the charset
value to be None
and the 'content-type'
value to be 'Text'
.
It follows the protocol RFC 7230 to change the value of charset
to ISO-8859-1
in this type of a situation. In case we are dealing with different types of encodings like 'utf-8',
we can explicitly specify the encoding by setting the property to Response.encoding
.
Requests support the usage of the full range of HTTP verbs which are defined in the following table. To most of the supported verbs, 'url'
is the only argument that must be passed while using them.
3.145.51.153