python requests response handling

Python Requests Response Handling

In Python, the requests module is commonly used to make HTTP requests to a server. Once the request has been made, the server sends a response back to the client. The requests module allows us to handle and manipulate these responses in various ways.

Response Object

When we make an HTTP request using the requests module, we get a response object in return. This object contains several attributes and methods that allow us to access and manipulate the data in the response.

Here is an example of how to make an HTTP GET request and access the response object:


import requests

response = requests.get('https://www.example.com')

print(response.status_code)
print(response.headers)
print(response.content)
    

In the above example, we make an HTTP GET request to 'https://www.example.com' using the get() method from the requests module. We then assign the response object to the variable response.

We can access various attributes of the response object such as:

  • status_code: The HTTP status code returned by the server.
  • headers: A dictionary containing the response headers.
  • content: The response body in bytes.
  • text: The response body as a string.
  • json(): Converts the JSON response body to a Python object.

Error Handling

When making HTTP requests, errors can occur at various stages such as DNS resolution, connection establishment, request sending, and response retrieval. The requests module provides several ways to handle these errors.

Here is an example of how to handle a request failure:


import requests

try:
    response = requests.get('https://www.example.com')
    response.raise_for_status()
except requests.exceptions.HTTPError as err:
    print(err)
    

In the above code, we make an HTTP GET request to 'https://www.example.com' and raise an exception if the response status code is not in the 200-299 range. The raise_for_status() method raises an HTTPError if the status code is not in the range.

Customizing Requests

The requests module provides several ways to customize HTTP requests by adding headers, parameters, and authentication credentials.

Here is an example of adding headers to an HTTP request:


import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}

response = requests.get('https://www.example.com', headers=headers)
    

In the above code, we create a dictionary of headers and pass it as a parameter to the get() method. The headers are used to identify the client making the request.