Requests - Handling History
Requests - Understanding Response History
Section titled “Requests - Understanding Response History”The response.history attribute in the Requests library provides valuable insight into the redirection process of an HTTP request. It records each step of a redirect chain.
The response.history Attribute
Section titled “The response.history Attribute”When a request results in one or more redirections, response.history will be a list of Response objects. Each object in this list represents a request-response cycle that occurred before the final response was received. The list is sorted from the oldest redirect to the most recent one, just before the final response.
If there were no redirections, response.history will be an empty list [].
Example: Inspecting Redirection History
Section titled “Example: Inspecting Redirection History”Let’s use a URL that we know will redirect multiple times to examine its history.
import requests
# This URL will redirect 3 times before landing on /geturl = 'http://httpbin.org/redirect/3'response = requests.get(url)
print(f"Final URL: {response.url}")print(f"Final Status Code: {response.status_code}")
print("\nRedirection History:")if response.history: for i, resp_in_history in enumerate(response.history): print(f" Step {i+1}:") print(f" Status Code: {resp_in_history.status_code}") print(f" URL: {resp_in_history.url}") print(f" Redirected from: {resp_in_history.headers.get('Location', 'N/A') if resp_in_history.is_redirect else 'N/A (not a redirect response)'}")else: print("No redirections occurred.")
print(f"\nRaw history list: {response.history}")Output
Section titled “Output”Final URL: http://httpbin.org/getFinal Status Code: 200
Redirection History: Step 1: Status Code: 302 URL: http://httpbin.org/redirect/3 Redirected from: /relative-redirect/2 Step 2: Status Code: 302 URL: http://httpbin.org/relative-redirect/2 Redirected from: /relative-redirect/1 Step 3: Status Code: 302 URL: http://httpbin.org/relative-redirect/1 Redirected from: /get
Raw history list: [<Response [302]>, <Response [302]>, <Response [302]>]By iterating through response.history, you can trace the entire path of redirections, including the status codes and intermediate URLs. This is particularly useful for debugging redirection issues or understanding how a specific URL resolves.
Each element in response.history is a full Response object, meaning you can access its headers, content (though usually empty for redirects), etc., just like the final response object.