Python Requests: A Versatile Tool for API Development

Prince Samuel
The Modern Scientist
4 min readFeb 27, 2023
Photo by Alex Chumak on Unsplash

Introduction

Python is a powerful programming language with a vast array of modules available for use. One of the most popular modules for performing HTTP requests in Python is the requests module. This module is highly regarded for its simplicity, ease of use, and flexibility. In this blog post, we will explore the requests module in Python and learn how to use it to send HTTP requests and handle responses.

Sending Requests with the Requests Module (request structure)

The primary use of the requests module is to send HTTP requests to a server and receive a response. The module supports various types of HTTP requests, including GET, POST, PUT, DELETE, and many more. Here's a list of all the request methods supported by the requests module:

  • requests.get(url, params=None, **kwargs)
  • requests.post(url, data=None, json=None, **kwargs)
  • requests.put(url, data=None, **kwargs)
  • requests.delete(url, **kwargs)
  • requests.head(url, **kwargs)
  • requests.options(url, **kwargs)
  • requests.patch(url, data=None, **kwargs)
  • requests.request(method, url, **kwargs)

To send a request, we first need to import the requests module and use its methods. For instance, to send a GET request, we can use the get() method as follows:

# Import the requests module
import requests

# Make a get() request
response = requests.get('<https://jsonplaceholder.typicode.com/posts>')

# Print the status / content of the request
print(response.status_code)
print(response.json())

In the code above, we send a GET request to a sample REST API and print the server’s response status code and JSON data. The response object returned by the get() method contains various attributes and methods for handling the response data, such as status_code, text, json(), etc.

Here are code examples for all the request methods:

GET Request

import requests

response = requests.get('https://jsonplaceholder.typicode.com/posts>')
print(response.status_code)
print(response.json())

POST Request

# Import the requests module
import requests

# the data we want to parse to the server
data = {
'title': 'some title',
'body': 'some body content',
'userId': 1}

# make the request - post the data to the server
response = requests.post('<https://jsonplaceholder.typicode.com/posts>', data=data)

# Print out the status code and the response content
print(response.status_code)
print(response.json())

PUT Request

# Import the requests module
import requests

# the updated data we want to parse to the server
data = {
'title': 'some new title',
'body': 'some new body content',
'userId': 1}

# make the request - update a given item
response = requests.put('<https://jsonplaceholder.typicode.com/posts/1>', data=data)

# Print out the status code and the response content
print(response.status_code)
print(response.json())

DELETE Request

# Import requests module
import requests

# make the requests - deletes an items from the server/db
response = requests.delete('<https://jsonplaceholder.typicode.com/posts/1>')

# Print the status code of the response
print(response.status_code)

HEAD Request

# import the request module
import requests

# make the request - returns the response header of the request
response = requests.head('<https://jsonplaceholder.typicode.com/posts>')

# print the status code of the response
print(response.status_code)

OPTIONS Request

# import the request module
import requests

# makes an options request to the server
response = requests.options('<https://jsonplaceholder.typicode.com/posts>')

# prints the status code of the response
print(response.status_code)

PATCH Request

# Import the requests module
import requests

# updated data - to be sent to the server
data = {'title': 'some updated title'}

# make the patch request - update an item
response = requests.patch('<https://jsonplaceholder.typicode.com/posts/1>', data=data)

# print status code and content of request
print(response.status_code)
print(response.json())

Custom Request

# Import the requests module
import requests

# set headers
headers = {'User-Agent': 'Mozilla/5.0'}

# make request - 'GET'
response = requests.request('GET', '<https://jsonplaceholder.typicode.com/posts>', headers=headers)

# Print status code of response
print(response.status_code)

Handling Responses with the Requests Module

Once we send a request, we need to handle the server’s response, which can be in various formats such as JSON, XML, HTML, etc. The requests module can automatically parse the response data based on its content type and return it in a convenient format. For instance, to parse JSON data, we can use the json() method as shown in the previous example. Similarly, to parse XML data, we can use the xml() method as follows:

# import relevant modules
import requests
from xml.etree.ElementTree import fromstring # this is used to handle xml responses

# make the request
response = requests.get('<https://www.w3schools.com/xml/note.xml>')

# handle the request - convert text response to xml
xml_data = fromstring(response.text)

# print out the response
print(xml_data.find('to').text)


## // NOTE THAT WE CAN ALSO USE THE json MODULE TO HANDLE REQUESTS

In the code above, we send a GET request to an XML endpoint and parse the response data using the xml() method. We then use the standard ElementTree library to extract the desired data from the XML tree.

Customizing Requests with the Requests Module

The requests module also provides various options for customizing requests, such as adding headers, query parameters, request body, etc. For instance, to add query parameters to a GET request, we can pass a dictionary to the params parameter as follows:

# Import the requests module
import requests

# query parameters
params = {'q': 'python'}

# make the request
response = requests.get('<https://www.google.com/search>', params=params)

# print response data
print(response.url)

In the code above, we add a query parameter q=python to a GET request to Google search and print the final URL. The requests module automatically encodes the query parameters and appends them to the URL.

Conclusion

The requests module is an essential tool for any Python developer who needs to interact with HTTP-based APIs or web services. It provides a simple and intuitive interface for sending requests, handling responses, and customizing requests. In this blog post, we have covered the basics of the requests module, including sending requests, handling responses, and customizing requests. With this knowledge, you can explore the vast possibilities of the module and build powerful applications that communicate with the web.

Like, comment, subscribe, and share for more tutorial articles on Python, Django, and Programming in General.

--

--

Prince Samuel
The Modern Scientist

Bsc. Computer Science, University of Ghana | Python | Django | API | JS | Dart | Flutter | Servers | Web