Revealing the structure of a request and response

A Requests object is the one which is created by the user when he/she tries to interact with a web resource. It will be sent as a prepared request to the server and does contain some parameters which are optional. Let us have an eagle eye view on the parameters:

  • Method: This is the HTTP method to be used to interact with the web service. For example: GET, POST, PUT.
  • URL: The web address to which the request needs to be sent.
  • headers: A dictionary of headers to be sent in the request.
  • files: This can be used while dealing with the multipart upload. It's the dictionary of files, with key as file name and value as file object.
  • data: This is the body to be attached to the request.json. There are two cases that come in to the picture here:
    • If json is provided, content-type in the header is changed to application/json and at this point, json acts as a body to the request.
    • In the second case, if both json and data are provided together, data is silently ignored.
  • params: A dictionary of URL parameters to append to the URL.
  • auth: This is used when we need to specify the authentication to the request. It's a tuple containing username and password.
  • cookies: A dictionary or a cookie jar of cookies which can be added to the request.
  • hooks: A dictionary of callback hooks.

A Response object contains the response of the server to a HTTP request. It is generated once Requests gets a response back from the server. It contains all of the information returned by the server and also stores the Request object we created originally.

Whenever we make a call to a server using the requests, two major transactions are taking place in this context which are listed as follows:

  • We are constructing a Request object which will be sent out to the server to request a resource
  • A Response object is generated by the requests module

Now, let us look at an example of getting a resource from Python's official site.

>>> response = requests.get('https://python.org')

In the preceding line of code, a requests object gets constructed and will be sent to 'https://python.org'. Thus obtained Requests object will be stored in the response.request variable. We can access the headers of the Request object which was sent off to the server in the following way:

>>> response.request.headers
CaseInsensitiveDict({'Accept-Encoding': 'gzip, deflate, compress', 'Accept': '*/*', 'User-Agent': 'python-requests/2.2.1 CPython/2.7.5+ Linux/3.13.0-43-generic'})

The headers returned by the server can be accessed with its 'headers' attribute as shown in the following example:

>>> response.headers
CaseInsensitiveDict({'content-length': '45950', 'via': '1.1 varnish', 'x-cache': 'HIT', 'accept-ranges': 'bytes', 'strict-transport-security': 'max-age=63072000; includeSubDomains', 'vary': 'Cookie', 'server': 'nginx', 'age': '557','content-type': 'text/html; charset=utf-8', 'public-key-pins': 'max-age=600; includeSubDomains; ..)

The response object contains different attributes like _content, status_code, headers, url, history, encoding, reason, cookies, elapsed, request.

>>> response.status_code
200
>>> response.url
u'https://www.python.org/'
>>> response.elapsed
datetime.timedelta(0, 1, 904954)
>>> response.reason
'OK'
..................Content has been hidden....................

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