- 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 - Working with Requests
In this chapter, we will understand how to work with the requests module. We will look into the following −
Making HTTP Requests.
Passing Parameters to HTTP Requests.
Making HTTP Requests
To make a Http request, we need to first import the request module as shown below −
import requests
Let us now see, how to make a call to URL using the requests module.
Let us use the URL −
in the code, to test Requests Module.Example
import requests getdata = requests.get() print(getdata.status_code)
The url −
is called using requests.get() method. The response object of the URL is stored in the getdata variable. When we print the variable, it gives the 200 response code, which means that we have got the response successfully.Output
E:prequests>python makeRequest.py <Response [200]>
To get the content from the response, we can do so using getdata.content as shown below −
Example
import requests getdata = requests.get() print(getdata.content)
getdata.content, will print all the data available in the response.
Output
E:prequests>python makeRequest.py b [ { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Sincere@april.biz", "address": { "street": "Kulas Light ", "suite": "Apt. 556", "city": "Gwenborough", "zipcode": " 92998-3874", "geo": { "lat": "-37.3159", "lng": "81.149 6" } }, "phone": "1-770-736-8031 x56442", "website": "hild egard.org", "company": { "name": "Romaguera-Crona", "catchPhr ase": "Multi-layered cpent-server neural-net", "bs": "harness real-time e-markets" } }
Passing Parameters to HTTP Requests
Just requesting the URL is not sufficient, we also need to pass parameters to the URL.
The params are mostly passed as key/value pair, for example −
So, we have id = 9 and username = Delphine. Now, will see how to pass such data to requests Http module.
Example
import requests payload = { id : 9, username : Delphine } getdata = requests.get(, params = payload) print(getdata.content)
The details are stored in the object payload in the key/value pair and passed to params, inside get() method.
Output
E:prequests>python makeRequest.py b [ { "id": 9, "name": "Glenna Reichert", "username": "Delphin e", "email": "Chaim_McDermott@dana.io", "address": { "street": "Dayna Park", "suite": "Suite 449", "city": "Bartholomebury", "zipcode": "76495-3109", "geo": { "lat": "24.6463", "lng": "-168.8889" } }, "phone": "(775)976-6794 x41206", " website": "conrad.com", "company": { "name": "Yost and Sons", "catchPhrase": "Switchable contextually-based project", "bs": "aggregate real-time technologies" } } ]
We are now getting the details of the id = 9 and username = Delphine details in the response.
If you want to see, how the URL looks after passing the parameters, making use of the response object to the URL.
Example
import requests payload = { id : 9, username : Delphine } getdata = requests.get(, params = payload) print(getdata.url)
Output
E:prequests>python makeRequest.pyAdvertisements