Requests - Handling Redirection
Requests - Handling Redirection
Section titled “Requests - Handling Redirection”This chapter explores how the Requests library handles URL redirections. By default, Requests will automatically follow redirections for GET, OPTIONS, POST, PUT, PATCH, DELETE, and HEAD requests.
Automatic Redirection and History
Section titled “Automatic Redirection and History”When a server responds with a redirect status code (like 301 Moved Permanently or 302 Found), Requests automatically makes a new request to the URL specified in the Location header. The Response object’s history attribute stores a list of Response objects that were part of this redirection chain, from oldest to most recent.
Example: Following Redirection
Section titled “Example: Following Redirection”import requests
# httpbin.org/redirect/N will redirect N timesurl = 'http://httpbin.org/redirect/2'response = requests.get(url)
print(f"Final URL: {response.url}")print(f"Status Code: {response.status_code}")print(f"History: {response.history}")
if response.history: print("Redirection path:") for resp_in_history in response.history: print(f" - Status: {resp_in_history.status_code}, URL: {resp_in_history.url}")Output
Section titled “Output”Final URL: http://httpbin.org/getStatus Code: 200History: [<Response [302]>, <Response [302]>]Redirection path: - Status: 302, URL: http://httpbin.org/redirect/2 - Status: 302, URL: http://httpbin.org/relative-redirect/1The response.history list contains Response objects. In this example, there were two redirections (both 302 Found status codes). The final URL is http://httpbin.org/get and has a status code of 200.
Disabling Redirection
Section titled “Disabling Redirection”You can prevent Requests from automatically following redirections by setting the allow_redirects parameter to False in your request. This applies to all HTTP methods.
Example: Disabling Redirection
Section titled “Example: Disabling Redirection”import requests
url = 'http://httpbin.org/redirect/1'response = requests.get(url, allow_redirects=False)
print(f"Status Code: {response.status_code}")print(f"History (should be empty): {response.history}")print(f"Location header: {response.headers.get('Location')}")# The content of a 3xx response often contains information about the redirect# print(f"Response Text:\n{response.text[:200]}...") # Show first 200 charsOutput
Section titled “Output”Status Code: 302History (should be empty): []Location header: /relative-redirect/0When allow_redirects is False, the request stops at the first redirect response. The status code is 302 (or another redirect code), response.history is empty, and you can inspect response.headers['Location'] to see where it would have redirected.