requests python follow redirects

How to Make Requests in Python Follow Redirects

If you're a Python developer who works with requests, you might have encountered situations where you need to follow redirects while making HTTP requests. In Python, you can easily make requests follow redirects by specifying a value for the allow_redirects parameter.

Method 1: Using the allow_redirects Parameter in requests.get()

The simplest way to make requests follow redirects is by passing True for the allow_redirects parameter in the requests.get() method:


import requests

response = requests.get('https://example.com', allow_redirects=True)

In this example, the HTTP GET request to https://example.com will follow any redirects returned by the server. By default, allow_redirects is set to True, so you don't need to explicitly pass it unless you want to disable redirects.

Method 2: Using the max_redirects Parameter in Session()

If you're making multiple requests and want to control the maximum number of redirects that can be followed across all requests, you can use the max_redirects parameter in a requests.Session() object:


import requests

session = requests.Session()
session.max_redirects = 5

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

In this example, we create a requests.Session() object and set its max_redirects attribute to 5. This means that any requests made through this session will follow up to 5 redirects before raising a requests.exceptions.TooManyRedirects exception.

If you don't set the max_redirects attribute, the session will follow redirects indefinitely.

Method 3: Using the hooks Parameter to Handle Redirects

If you need more control over how redirects are handled, you can use the hooks parameter in requests.Session() or requests.get(). This allows you to register a callback function that gets called every time a redirect happens:


import requests

def handle_redirects(response, **kwargs):
    if response.is_redirect:
        print(f'Redirected to: {response.headers["Location"]}')

session = requests.Session()
session.hooks['response'].append(handle_redirects)

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

In this example, we define a callback function called handle_redirects() that gets called every time a redirect happens. The function checks if the response is a redirect (i.e., has a 3xx status code) and prints the redirect URL to the console.

We then create a requests.Session() object and register the callback function with the 'response' event using the hooks dictionary. Finally, we make a GET request to https://example.com. Every time a redirect happens, the handle_redirects() function will be called and print the redirect URL to the console.

Conclusion

In this post, we've explored three different ways to make requests follow redirects in Python using the allow_redirects parameter, the max_redirects attribute, and the hooks parameter. Depending on your use case, you can choose the method that works best for you.