Python ModulesRequests TutorialRedirection and History

Redirection and History with Requests

When working with HTTP requests, redirection often occurs, where a server responds with a new location for the requested resource. The requests library simplifies handling such scenarios and provides tools to inspect the history of redirects.


Managing Redirects

By default, the requests library automatically follows redirects. You can control this behavior using the allow_redirects parameter.

Example: Following Redirects

import requests
 
# URL that redirects to another URL
url = "http://example.com/redirect"
response = requests.get(url)
 
# Print the final URL after redirection
print("Final URL:", response.url)

Example: Disabling Redirects

response = requests.get(url, allow_redirects=False)
 
# Check the status code and headers
print("Status Code:", response.status_code)
print("Location Header:", response.headers.get("Location"))

When allow_redirects=False, the response will not follow redirects, and the Location header can be inspected to determine the next URL.


Accessing Request History

The response.history attribute contains a list of Response objects representing each step in the redirection chain.

Example: Inspecting Request History

response = requests.get(url)
 
# Check if there were any redirects
if response.history:
    print("Redirection history:")
    for resp in response.history:
        print(f"{resp.status_code} -> {resp.url}")
 
# Final URL after all redirects
print("Final URL:", response.url)

Practical Considerations

Handling Redirect Loops

Redirect loops occur when a server keeps redirecting between URLs endlessly. The requests library raises a TooManyRedirects exception after a reasonable number of redirects (default: 30).

Example: Handling Redirect Loops

from requests.exceptions import TooManyRedirects
 
try:
    response = requests.get(url)
except TooManyRedirects as e:
    print("Redirect loop detected:", e)

Debugging Redirection Issues

Use logging to inspect redirect behavior and debug issues.

import logging
import requests
 
# Enable logging
logging.basicConfig(level=logging.DEBUG)
response = requests.get(url)

By understanding how to manage redirects and access request history, you can build more robust applications using the requests library.