API Testing Using Python Script
Are you trying to test API in your project without a tool? Here comes the Requests library, one of the powerful and standard libraries in Python.
Join the DZone community and get the full member experience.
Join For FreeAre you trying to test API in your project without a tool? Here comes the Requests library, one of the powerful and standard libraries in Python.
Overview:
In this article, we will understand how to do API testing using simple python scripts. We shall make use of the Requests library of python to handle HTTP requests. The simple import of this library and any IDE ( eclipse,pycharm, IntelliJ) would suffice, and there is no need for any specific tool to accomplish API testing.
We shall cover the following topics in this article:
- Install Requests library
- Implement HTTP GET request
- Implement HTTP POST request
- Implement HTTP PUT request
- Implement HTTP DELETE request
Install Requests in Python:
Run the Command “python -m pip install requests” in the Command Prompt.
Requests library comes with different features, which will make testing of HTTP requests much easy and flexible. We can implement all the HTTP methods, extract content from headers, verify authorization, and customize the requests by sending different query parameters and body messages.
We shall use the Eclipse IDE ( Pydev plugin added) to execute different HTTP requests.
For this demo, I have installed a small application on NodeJS, which has few API requests in JSON format. In this sample application, I have added 3 resources Tutorial, subscriptions, and comments. This sample API can take tutorial id and provide the tutorial details as a response
Let us discuss how we can implement different HTTP methods on this sample application using python.
1. Implement HTTP GET Request:
Let us execute the most used HTTP method, “GET,” which retrieves the requested resource data. We can also send query parameters to retrieve specific data.
Console: Here is the response after invoking the requested URL.
- We can also modify the GET request by adding query parameters. Here in the same example, if we would want to retrieve tutorial details for 1006, then provide as below:
- I have created a variable response_code in which the results are captured. Here response_code carries the return status code, i.e., 200 in this example.
- In case we want the response details, we can use the .json() method whose return type is a dictionary ( key-value pairs).
Similarly, we can fetch the response header details using .headers and .cookies
2. Implement HTTP POST Request:
To execute the POST method, there is a need to send data through the message body. The data can be sent using the data parameter within the Post method.
In the below example, we have added data parameters in the post method.If we see the response, the return code is 201 (which implies a record is created), and the record is added.
3. Implement HTTP PUT Request:
PUT request behaves in two different ways. If the requested resource is already existing, it updates the details; it creates the new record. We can pass the data as a body parameter.
4. Implement HTTP DELETE Request:
Delete request deletes the specified resource identified in the request URI. We can send that specific id either as part of URI or by using param(parameters).
Conclusion:
We have now explored and implemented different methods within the API. These are few features of the Requests library in python.
Opinions expressed by DZone contributors are their own.
Comments