Saturday, November 16, 2024
Google search engine
HomeLanguagesCreate API Tester using Python Requests Module

Create API Tester using Python Requests Module

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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments