[파이썬] requests 요청 시 리다이렉트 처리

When working with web APIs or making HTTP requests in Python, it is common to encounter redirects. A redirect occurs when a web server responds with a specific status code indicating that the requested resource has been moved to a different URL. In such cases, it becomes necessary to handle the redirection and follow it to the new location. This is where the Python library Requests comes in handy.

Understanding Redirects

HTTP redirects are denoted by the status codes starting with 3, such as 301 Moved Permanently or 302 Found. These codes indicate that the requested resource has been relocated. The server includes the new location URL in the response headers, allowing the client to automatically redirect the request to the new location.

The Requests Library

Requests is a popular and powerful HTTP library for Python that simplifies sending HTTP requests and handling responses. It provides various methods to perform GET, POST, PUT, DELETE, and other HTTP requests. Additionally, it includes built-in support for handling redirects.

Redirect Handling in Requests

When using the Requests library, redirection is automatically handled by default. When a redirect response is encountered, Requests follows the redirect and returns a new response object with the updated information.

Here’s an example code snippet that demonstrates making a GET request and handling redirects using the Requests library:

import requests

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

# Check if the request was redirected
if response.history:
    print("Request was redirected")
    for resp in response.history:
        print(resp.status_code, resp.url)
    
    print("Final destination:")
    print(response.status_code, response.url)
else:
    print("Request was not redirected")

In the example above, we start by importing the Requests library. We then specify the URL we want to request and use the requests.get() method to make a GET request to that URL. The response object is stored in the response variable.

Next, we check if the response has a history. If it does, it means that the request was redirected. We loop through each redirect response in the history and print the status code and the URL. Finally, we print the status code and URL of the final destination.

This simple code snippet demonstrates how Requests handles redirects and provides access to the entire redirect history.

Customizing Redirect Handling

By default, Requests automatically follows redirects. However, you can customize this behavior by using the allow_redirects parameter when making a request. If you set allow_redirects=False, the library will not automatically follow redirects and return the redirect response as it is.

import requests

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

print(response.status_code)  # Prints the redirect status code
print(response.headers['Location'])  # Prints the redirect location URL

In the example above, we disable automatic redirects by setting allow_redirects=False in the requests.get() method. This allows us to fetch the redirect response without following it. We then print the status code and the location URL.

This customization can be useful in scenarios where you want to manually handle redirects or extract specific information from the redirect response.

Conclusion

Handling redirects is an essential part of working with web APIs and making HTTP requests. The Requests library in Python simplifies this task by automatically following redirects and providing access to the redirect history. By understanding how redirects work and utilizing the capabilities of the Requests library, you can effectively handle redirects in Python and ensure smooth web interactions in your applications.