python requests allow_redirects=true

What is python requests allow_redirects=true?

Python requests allow_redirects=true is a parameter that can be passed to the requests library in Python to allow the redirection of HTTP requests. When set to true, this parameter enables the library to follow any redirections that may be encountered during an HTTP request.

How does it work?

When a user sends an HTTP request to a server, the server may sometimes respond with a redirect response code. This code will contain a new URL that the user needs to visit to continue their request. In some cases, this redirect may be necessary to complete the request, for example, when accessing a secure resource.

By default, the requests library will not follow these redirects and will instead raise an exception. However, by setting the allow_redirects parameter to true, the library will automatically follow the redirect and send the request to the new URL provided in the response.

Example Usage

import requests

# Make a simple HTTP request
response = requests.get('')

# Print the status code of the response

# Make the same request but allow redirects
response = requests.get('', allow_redirects=True)

# Print the status code of the redirected response

Alternate Usage

Another way to use this parameter is by passing it as an argument in a session object. This way, all requests made with that session will automatically follow any redirects.

import requests

# Create a session object
session = requests.Session()

# Set the allow_redirects parameter to true for all requests
session.allow_redirects = True

# Make a request using the session object
response = session.get('')

# Print the status code of the redirected response