When making HTTP requests with the Python Requests library, you may encounter redirections where the server responds with a 3xx redirect status code telling your client to fetch the content from a different URL.
By default, Requests will automatically follow redirections, but you can override this behavior. Here's how to disable automatic redirects in Requests:
import requests
response = requests.get('http://example.com', allow_redirects=False)
Setting
This means if the server responds with a 302 or 301 redirect pointing to a different location, Requests will simply return that response object to you rather than fetching the resource from the new location.
Some cases where you may want to disable redirects:
When working with redirects, you can check the response status code to see if a redirect occurred:
if response.status_code in (301, 302):
# Redirect occurred
And to then manually follow the redirect location specified in the
location = response.headers['Location']
response = requests.get(location)
Controlling redirects gives you more visibility into exactly what requests are made. Disable auto redirects in Python Requests using