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.
- 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.
- 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));
- 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.
According to Postman API, the table contains the status code that it offers.
Code Range | Description | Example |
---|---|---|
2– | Success |
|
3– | Redirection |
|
4– | Client Error |
|
5– | Server Error |
|
How to test an API using Postman ?
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.