Iterating over streaming APIs

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.

Note

It's better to keep in mind that it's not always safe to call the iter_lines() function as it may result in loss of received data.

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.

Encodings

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.

HTTP verbs

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.

Method

Description

GET

GET method requests a representation of the specified resource. Apart from retrieving the data, there will be no other effect of using this method.

Definition is given as requests.get(url, **kwargs)

POST

The POST verb is used for the creation of new resources. The submitted data will be handled by the server to a specified resource.

Definition is given as requests.post(url, data=None, json=None, **kwargs)

PUT

This method uploads a representation of the specified URI. If the URI is not pointing to any resource, the server can create a new object with the given data or it will modify the existing resource.

Definition is given as requests.put(url, data=None, **kwargs)

DELETE

This is pretty easy to understand. It is used to delete the specified resource.

Definition is given as requests.delete(url, **kwargs)

HEAD

This verb is useful for retrieving meta-information written in response headers without having to fetch the response body.

Definition is given as requests.head(url, **kwargs)

OPTIONS

OPTIONS is a HTTP method which returns the HTTP methods that the server supports for a specified URL.

Definition is given as requests.options(url, **kwargs)

PATCH

This method is used to apply partial modifications to a resource.

Definition is given as requests.patch(url, data=None, **kwargs)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.51.153