Create API Tester using Python Requests Module
Last Updated :
15 Mar, 2023
Prerequisites: Python Requests module, API
In this article, we will discuss the work process of the Python Requests module by creating an API tester.
API stands for Application Programming Interface (main participant of all the interactivity). It is like a messenger that takes our requests to a system and returns a response back to us via seamless connectivity. We use APIs in many cases like to get data for a web application or to connect to a remote server that has data like weather that keeps changing or to enable two applications to exchange data among each other.
The requests library is one of an integral part of Python for making HTTP requests to a specified URL. Whether it be REST APIs or Web Scraping, requests are must be learned for proceeding further with these technologies. When one makes a request to a URI, it returns a response. Python requests provide inbuilt functionalities for managing both the request and response.
Step-by-step Approach:
- First, we choose input from the user what they want.
- Take an input URL.
If the user chooses GET Request.
- In getReq() function, make a GET Request using the Requests module and store the result.
- Format the result using the JSON module and return it.
If the user chooses Post Request.
- Take a dictionary data to send to the server.
- In postReq() function, make a POST Request with JSON data using the Request module and store the result.
- Format the result using the JSON module and return it.
Finally, print the returned result.
Below is the implementation based on the above approach:
Python3
# request module to
# work with api's
import requests
# json module to
# parse and get json
import json
# This function are return the
# json response from given url
def getReq(url):
# handle the exceptions
try:
# make a get request using requests
# module and store the result.
res = requests.get(url)
# return the result after
# formatting in json.
return json.dumps(res.json(), indent=4)
except Exception as ee:
return f"Message : {ee}"
# This function are return the
# json response of url and json
# data you send the server
def postReq(url, data):
# handle the exceptions
try:
# make a post request with
# the json data
res = requests.post(url, json=data)
# return the response after
# formatting in json.
return json.dumps(res.json(), indent=4)
except Exception as er:
return f"Message : {er}"
# Driver Code
if __name__ == '__main__':
# always run loop to make
# menu driven program
while True:
# handle the exceptions
try:
choice = int(input("1.Get Request\n2.Post Request\n3.Exit\nEnter Choice : "))
# get user choice and perform tasks.
if choice == 1:
# take a url as a input.
url_inp = input("Enter a valid get url : ")
# print the result of the url.
print(getReq(url_inp))
elif choice == 2:
# take a url as a input
url_inp = input("Enter a valid get url : ")
# take a formal data as input in dictionary.
data_inp = {
"name": input("Name : "),
"email": input("Email : "),
"work": input("Work : "),
"age": input("Age : ")
}
# print the result.
print(postReq(url_inp, data_inp))
elif choice == 3:
# if user want to exit.
exit(0)
except Exception as e:
print("Error : ", e)
Output:
First, make the get request

A get request with the query.

A get request to fetch all users.

Lastly, make a post request with JSON data.

Below is the complete output video to depict the functionality of the program
Similar Reads
GET and POST Requests Using Python This post discusses two HTTP (Hypertext Transfer Protocol) request methods  GET and POST requests in Python and their implementation in Python. What is HTTP? HTTP is a set of protocols designed to enable communication between clients and servers. It works as a request-response protocol between a cli
7 min read
Authentication using Python requests Authentication refers to giving a user permissions to access a particular resource. Since, everyone can't be allowed to access data from every URL, one would require authentication primarily. To achieve this authentication, typically one provides authentication data through Authorization header or a
2 min read
response.status_code - Python requests response.status_code returns a number that indicates the status (200 is OK, 404 is Not Found). Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object. Now, this response object
2 min read
GET and POST Requests in GraphQL API using Python requests In this article, we will be understanding how to write GET and POST requests to GRAPHQL APIs using the Python request module. Dealing with GraphQL API is a bit different compared to the simple REST APIs. We have to parse in a query that involves parsing the GraphQL queries in the request body. What
9 min read
Http Request methods - Python requests Python requests module has several built-in methods to make Http requests to specified URI using GET, POST, PUT, PATCH or HEAD requests. A Http request is meant to either retrieve data from a specified URI or to push data to a server. It works as a request-response protocol between a client and serv
7 min read
How to create and write tests for API requests in Postman? Postman is an API(utility programming interface) development device that enables to construct, take a look at and alter APIs. It could make numerous varieties of HTTP requests(GET, POST, PUT, PATCH), store environments for later use, and convert the API to code for various languages(like JavaScript,
3 min read
Response Methods - Python requests When one makes a request to a URI, it returns a response. This Response object in terms of python is returned by requests.method(), method being - get, post, put, etc. Response is a powerful object with lots of functions and attributes that assist in normalizing data or creating ideal portions of co
5 min read
Session Objects - Python requests Session object allows one to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance and will use urllib3âs connection pooling. So, if several requests are being made to the same host, the underlying TCP connection will be reused, which
2 min read
How to Make API Call Using Python APIs (Application Programming Interfaces) are an essential part of modern software development, allowing different applications to communicate and share data. Python provides a popular library i.e. requests library that simplifies the process of calling API in Python. In this article, we will see ho
3 min read
GET method - Python requests Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make GET request to a specified URL using requests.GET() method. Before checking out GET method, let's figure out what a GET request is - GET Http Method T
2 min read