requests do not follow redirects

What are requests that do not follow redirects?

Requests are a type of HTTP client library that allows us to send HTTP/1.1 requests extremely easily. These requests can be used to interact with APIs or to obtain data from web pages. However, sometimes when we make a request to a server, the server may respond with a redirect (e.g., a 301 or 302 status code). In this case, the server is telling the client to resend the request to a different URL.

By default, when we send a request using the requests library, it will automatically follow redirects. That means that if we make a request to a URL that redirects, the library will automatically resend the request to the new URL. However, there are situations where we may not want this behavior. For example, we may want to preserve the original URL or we may not have permission to follow redirects.

How to prevent requests from following redirects?

If you want to prevent requests from following redirects, you can do so by setting the allow_redirects parameter to False when making the request. Here is an example:


import requests

url = 'http://example.com'
response = requests.get(url, allow_redirects=False)

print(response.status_code)

In this example, we are setting allow_redirects=False, which will prevent requests from following any redirects. Instead, the response will contain information about the original redirect status code and location header. We can use this information to manually follow the redirect if needed.

How to handle requests that do not follow redirects?

If you need to handle requests that do not follow redirects, you can use the history attribute of the response object. This attribute contains a list of all the response objects that were generated during the redirect chain. Here is an example:


import requests

url = 'http://example.com'
response = requests.get(url, allow_redirects=False)

if response.status_code == 301 or response.status_code == 302:
    print("Redirect detected!")
    for r in response.history:
        print(f"Redirected from {r.url} to {r.next.url}")
else:
    print("No redirect detected!")

In this example, we are checking the status code of the response. If it is a redirect status code, we are printing out information about the redirect chain using the history attribute.

Conclusion

Requests library is a great tool for making HTTP requests, but sometimes we need to handle redirects in a different way. By setting allow_redirects=False, we can prevent requests from following redirects and use the history attribute to manually follow the redirect chain if needed.