Response methods in Python requests

Many times we need to perform operations related to the web. Because Python is easy and simple to use it supports a Request API that provides many features to interact with the web or web pages. For example, making an HTTP request and many useful features or methods inside the requests library. When an URL is called using requests.get() it returns a response object which is useful for interaction and getting relevant information data from the web using methods such as response.status_code(), response.content(), etc. In this tutorial we will learn how to:

– Make requests using the most common HTTP methods.
– Inspect data from your requests and responses.

Response methods in Python

Let’s start…

Install the requests library to our environment first.

!pip install requests

requests library supports many methods such as get, post, etc. To perform an action from the specific resource we then invoke the method We will learn the services provided by each method. The various methods are:

1. The GET request :
The get() method indicates that the requests API is ready to retrieve the data from the resource specified.
for example: Getting an HTTP request and saving it in a response variable also called a response object.

import requests
response = requests.get('https://api.github.com')

Requests library provides various services. Now that we have created the response object we are ready to perform the actions on the HTTP link using those services.

2. Status codes :
It returns the information about the status of the request.

response.status_code
output: 200

200 means that the request has been successfully accessed.

3. The content:
However, after successfully making an HTTP request, we can use the content method to get valuable information from the Response object.

response.content

Because this is in Bytes format we should convert this into the string. python provides .text to get the information explicitly in a string format.

response.text

Now we may want to load retrieved information in a JSON format. Thus we can use .json() method provided by Response object.

response.json()

4. Headers :
The headers method returns some useful information about the data in the form of a dictionary. For example, the content type of the Response payload and a time limit on how long to cache the response, etc.

response.headers

Other Response methods

Some other important Response methods are:

  • response.encoding – It returns the encoding used to decode response.content.
  • response.elapsed – It returns a timedelta object i.e time taken for execution.
  • response.close() – It closes the server connection.
  • response.cookies – It returns a CookieJar object from the server.
  • response.history – It returns all list of Response objects used for making HTTP request or URL.
  • response.is_permanent_redirect – If the redirected URL is permanent it returns TRUE else it returns False.
  • response.is_redirect –¬† If the URL was redirected it returns TRUE else it returns False.
  • response.iter_content() – It returns the iterable object of response.content
  • response.json() – It returns response object in JSON format.
  • response.url – It returns the URL address of the response object.
  • response.request – It returns the server response used for requesting the URL or response object
  • response.reason – It returns a ¬†short textual description corresponding to the status code.
  • response.raise_for_status() – If an error has occurred during the process it returns an HTTPError object.
  • response.links – It is used to access the header of the links.

Leave a Reply

Your email address will not be published. Required fields are marked *