How to test an API using Postman ?
Last Updated :
01 Apr, 2024
API testing is a software testing type that tends to validate the application programming interfaces. As per Postman API, API testing is confirming that an API is working as expected. It sends requests to an API and monitors the responses to check its behavior to assess the functionality, reliability, performance, and security of an API. It is usually considered to be a crucial part of the API development lifecycle.
What is Postman API?
Postman recently ranked on top for best API Platform, by G2 in its 2023 Spring Reports. It is easy and user-friendly and offers various courses and projects. To use it, one must register in the Postman platform. As per Postman, it is an API platform for building and using APIs that simplifies each step of the API lifecycle and streamlines collaboration so one can create better APIs. It is trusted by over 25 million users worldwide. It has a friendly community too and allows API testing with REST.
History of Postman:
Just like every origin story, the origin, purpose, development till date has many theories but web based APIs tend to be more recent and the example can be Google Maps, something we use pretty often. Postman API was created by Abhinav Asthana, Ankit Sobti and Abhijit Kane in Bangalore, India (2012). Firstly, it was developed as a plugin for Google Chrome, then a rich client, and finally a thin client and now, Postman Inc., originally from India, has its headquarters in San Francisco.
Syntax:
<Request method> <Request url>
Example of API Testing using Postman
How about a bookstore or maybe Library API? We'll test that in this example. Firstly, get acquainted with the basics of Postman API and API testing fundamentals first like HTTP methods, mostly. You need to create an account in the Postman API platform.
- Download and create an account on Postman API. You can also use their workspace online.
- Create a new workspace and name it accordingly like postman library API.
- Next we will create a new request, name it, POST. I wanted to add books so I created this request.
- Choose POST as the request method.
- Enter the API endpoint URL in the address bar. You can also save it as baseURL. You need to save it as a variable first. Let's assume the baseURL has a current value as “https://library-api.postmanlabs.com”.
- Type in the following code snippet in body. Click the "Send" button to execute the request.
Enter any book details in the code snippet- And then check the response in the "Response" window. You should be able to view book details. Now for API testing, first, go to the "Tests" tab.
Response window at bottom showing book details- Look for the code bracket icon. Select JavaScript Fetch. Write JavaScript code for fetch response is, like:
JavaScript
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("api-key", "postmanrulz");
const raw = JSON.stringify({
"title": "To Kill a Mockingbird New",
"author": "Harper Lee",
"genre": "fiction",
"yearPublished": 1960
});
const requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow"
};
fetch("https://library-api.postmanlabs.com/books", requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.error(error));
- For test body, write a code snippet like:
const id = pm.response.json().id
pm.collectionVariables.set("id", id)
- Click the "Run" button in the "Tests" tab to execute the code snippet. The results for the test are displayed almost instantly. Check and evaluate accordingly.
Output: Since the API Testing was successful. It shows the response status and body.
The code 201 ishighlighted in green.According to Postman API, the table contains the status code that it offers.
Code Range
| Description
| Example
|
---|
2--
| Success
| - 200 - OK
- 201 - Created
- 204 - No content but OK
|
3--
| Redirection
| |
4--
| Client Error
| - 400 - Bad request
- 401 - Unauthorized
- 403 - Not Permitted
- 404 - Not Found
|
5--
| Server Error
| - 500 - Internal server error
- 502 - Bad gateway
- 504 - Gateway timeout
|
Key Features:
- It can build various API requests like, GET, POST, PUT, DELETE, etc. with user-friendly interfaces for defining URLs, headers, body parameters, and authentication methods.
- It can send, analyze and execute requests easily.
- It has simple and neat documentations.
- Write tests using JavaScript or Collections/Environments that validates response codes, body content, headers, and other aspects. You can group them, and execute entire collections of tests at once using the collection runner to streamline automated testing.
- Share collections and/or test scripts with team members via workspaces or public links to enable collaborative API development.
- Manage environment variables to store and reuse values like API keys, base URLs, across requests and tests, enabling maintainability and reusability.
- It has Mock servers that simulates API responses for development and testing purposes, allowing you to work on API integration without relying on external endpoints.
- It can track API performance metrics like, response times, errors over time to identify potential issues and optimize performance.
Advantages:
- It has neat, user friendly interface.
- It tends to organize and group API requests (in Postman API collections) efficiently.
- It can monitor and analyse by tracking API performance, response times etc.
Disadvantages:
- There might be few paid tools or features in this platform.
- They always need to add an additional server to the user's local environment.
- Storing tokens is not safe in Postman API.
Similar Reads
How to create routes using Express and Postman?
In this article we are going to implement different HTTP routes using Express JS and Postman. Server side routes are different endpoints of a application that are used to exchange data from client side to server side. Express.js is a framework that works on top of Node.js server to simplify its APIs
3 min read
How to Replicate Postman POST API Request using AJAX ?
In this project we'll mimic the action of submitting data using POST to a server from a web page. We replicate this process using AJAX (Asynchronous JavaScript and XML) directly in a web page, we're embedding similar functionality within our own interface. This involves setting up HTML elements like
4 min read
How To Generate Reports Using Postman Tool ?
Postman is a widely used tool for API testing. It offers a user-friendly interface for sending various HTTP requests (GET, POST, PUT, DELETE). One of the best things that we can do in Postman is Generate reports effectively. In this article, we will explore the various approaches to generating repor
3 min read
Testing REST API with Postman and curl
In the world of API testing, there are many tools available. Postman and cURL are two of the most popular tools for it. Let's look at how to use these tools for testing them. We will send some HTTP requests and explore the basic syntax for both of them in this article. The article focuses on using a
7 min read
How to Use Postman Online for API Testing
Postman is a popular form for testing and is active among APIs, whether you're a planner or an exploratory. Postman Online, as known or named at another time or place, is a variant of Postman that runs in your network internet or web viewing software, assigning you API testing and cooperation tasks
7 min read
Postman vs Apidog for API Testing
API testing is the practice of ensuring that an API works as intended. It can be conducted manually by developers or automated using an API testing tool. There are several sorts of API testing, and each one serves a unique role in guaranteeing the API's reliability. In this article, we'll learn abou
5 min read
How to write automated tests for APIs using Postman
Postman is an API (Application Programming Interface) development tool that helps developers test and manage API responses while developing or modifying an API. Along with a dynamic user interface and manual testing features, postman also comes up with a feature of writing automated tests to verify
5 min read
How to make Automation Projects using Postman ?
In this article, we will be seeing how you can use postman to make automation projects. This article will also help many developers and Penetration testers instead of Burp suite. You can also call postman interceptor as the mini version of Brupsuite Interceptor. Postman is a cutting-edge Automation
2 min read
How to create mock servers using Postman
Postman is a comprehensive API platform used by developers; this platform provides a set of tools that support API design, testing, documentation, mocking, and API Monitoring. It also simplifies each step of the API lifecycle and streamlines collaboration, enabling developers to create and use APIs
7 min read
How to generate automated test reports using Postman ?
Postman is the API testing tool that helps to simplify the process of developing and testing APIs (Application Programming Interface). In this article, we will explore how to not only create environments but also generate automated test reports, enhancing efficiency and reliability in the API testin
2 min read