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 − https://jsonplaceholder.typicode.com/users in the code, to test Requests Module.

Example

import requests
getdata = requests.get('https://jsonplaceholder.typicode.com/users')
print(getdata.status_code)

The url − https://jsonplaceholder.typicode.com/users 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('https://jsonplaceholder.typicode.com/users')
print(getdata.content)

getdata.content, will print all the data available in the response.

Output

E:\prequests>python makeRequest.py
b'[\n {\n  "id": 1,\n  "name": "Leanne Graham",\n  "username": "Bret",\n
"email": "Sincere@april.biz",\n  "address": {\n  "street": "Kulas Light
",\n  "suite": "Apt. 556",\n  "city": "Gwenborough",\n  "zipcode": "
92998-3874",\n  "geo": {\n "lat": "-37.3159",\n  "lng": "81.149
6"\n }\n },\n  "phone": "1-770-736-8031 x56442",\n  "website": "hild
egard.org",\n  "company": {\n "name": "Romaguera-Crona",\n  "catchPhr
ase": "Multi-layered client-server neural-net",\n  "bs": "harness real-time
e-markets"\n }\n }

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 −

 https://jsonplaceholder.typicode.com/users?id=9&username=Delphine

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('https://jsonplaceholder.typicode.com/users', 
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'[\n {\n "id": 9,\n "name": "Glenna Reichert",\n "username": "Delphin
e",\n "email": "Chaim_McDermott@dana.io",\n "address": {\n "street":
"Dayna Park",\n "suite": "Suite 449",\n "city": "Bartholomebury",\n
"zipcode": "76495-3109",\n "geo": {\n "lat": "24.6463",\n
"lng": "-168.8889"\n }\n },\n "phone": "(775)976-6794 x41206",\n "
website": "conrad.com",\n "company": {\n "name": "Yost and Sons",\n
"catchPhrase": "Switchable contextually-based project",\n "bs": "aggregate
real-time technologies"\n }\n }\n]'

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('https://jsonplaceholder.typicode.com/users', 
params = payload)
print(getdata.url)

Output

E:\prequests>python makeRequest.py
https://jsonplaceholder.typicode.com/users?id=9&username=Delphine
Advertisements