Requests Tutorial
Selected Reading
- Requests - Discussion
- Requests - Useful Resources
- Requests - Quick Guide
- Requests - Web Scraping using Requests
- Requests - Proxy
- Requests - Event Hooks
- Requests - Authentication
- Requests - SSL Certification
- Requests - Handling Sessions
- Requests - Handling History
- Requests - Handling Redirection
- Requests - Handling Timeouts
- Requests - Working with Errors
- Requests - Working with Cookies
- Requests - File Upload
- Handling POST, PUT, PATCH & DELETE Requests
- Requests - Handling GET Requests
- Requests - HTTP Requests Headers
- Handling Response for HTTP Requests
- Requests - Working with Requests
- Requests - How Http Requests Work?
- Requests - Environment Setup
- Requests - Overview
- Requests - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Requests - Handling Redirection
Requests - Handpng Redirection
This chapter will take a look at how the Request pbrary handles the url redirection case.
Example
import requests getdata = requests.get() print(getdata.status_code) print(getdata.history)
The url:
will be redirected using status code 301(Moved Permanently) to . The redirection will be saved in the history.Output
When the above code is executed, we get the following result −
E:prequests>python makeRequest.py 200 [<Response [301]>]
You can stop redirection of a URL using allow_redirects = False. It can be done on GET, POST, OPTIONS, PUT, DELETE, PATCH methods used.
Example
Here is an example on the same.
import requests getdata = requests.get(, allow_redirects=False) print(getdata.status_code) print(getdata.history) print(getdata.text)
Now if you check the output, the redirection will not be allowed and will get a status code of 301.
Output
E:prequests>python makeRequest.py 301 [] <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>301 Moved</TITLE></HEAD><BODY> <H1>301 Moved</H1> The document has moved <A HREF="http://www.google.com/">here</A>. </BODY></HTML>Advertisements