API testing ensures that an Application Programming Interface (API) works as expected. Tools like Postman allow developers to send HTTP requests (GET, POST, PUT, DELETE, etc.), validate responses and automate test cases. This helps assess the functionality, reliability, performance and security of an API.
What is Postman
Postman is a widely used API testing and development tool that helps developers send HTTP requests, validate responses, and automate API testing through a simple interface.
- Supports HTTP methods like GET, POST, PUT, and DELETE
- User-friendly interface for building and testing APIs
- Organizes requests using collections
- Uses environment variables for dynamic data
- Allows automated testing with JavaScript
- Provides mock servers, monitoring, and team collaboration
Prerequisites: Download Postman.
Syntax of an API Request in Postman
<Request Method> <Request URL>
Example:
GET https://api.example.com/users
Step-by-Step Example: Testing an API
Let’s assume we have a Library API with the base URL:
https://library-api.postmanlabs.com
Step 1: Create a New Workspace
- Open Postman
- Click Workspaces -> Create Workspace
- Name it Library API Testing
- Click Creat
Step 2: Create a New Request
- Click New -> Request.
- Choose a request type (e.g., POST to add a new book).
- Enter the URL:
https://library-api.postmanlabs.com/books
Step 3: Add Request Body
- Go to the Body tab
- Select raw
- Choose JSON
- Enter the following data:
{
"title": "To Kill a Mockingbird New" ,
"author": "Harper Lee",
"genre": "Fiction",
"yearPublished": 1960
}
-(1).png)
Step 4: Send the Request
- Click Send.
- Check the Response window at the bottom.
- If successful, you’ll see:
- Status Code: 201 Created
- Response Body with book details.
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.
-(1).png)
Look for the code bracket icon. Select JavaScript Fetch. Write JavaScript code for fetch response is, like:
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));
Step 5: Write Tests in Postman
- Go to the Tests tab
- Add the following code:
const id = pm.response.json().id
pm.collectionVariables.set("id", id)
- Click Run to execute the test
This script extracts the id from the response and stores it as a collection variable for future requests.
Output: Since the API Testing was successful. It shows the response status and body.
-(1)-(1).png)
Common HTTP Status Codes in API Testing
| Status Code | Type | Meaning |
|---|---|---|
| 200 | Success | OK |
| 201 | Success | Resource Created |
| 204 | Success | No Content |
| 400 | Client Error | Bad Request |
| 401 | Client Error | Unauthorized |
| 403 | Client Error | Forbidden |
| 404 | Client Error | Not Found |
| 500 | Server Error | Internal Server Error |