Open In App

How to test an API using Postman ?

Last Updated : 01 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.
Screenshot-(189)-(1)

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.
Screenshot-(190)-(1)

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.

Screenshot-(186)-(1)-(1)

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

  • 301 – Moved/Changed path

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads