Learn how to test REST APIs using Postman, HTTP methods, request bodies, response status codes, and automated testing tools.
API testing helps developers verify that backend applications work correctly before connecting them to a frontend application.
| Tool | Purpose |
|---|---|
| Postman | Send HTTP requests and inspect API responses. |
| Thunder Client | Test APIs directly inside Visual Studio Code. |
| curl | Send HTTP requests from the terminal. |
| Supertest | Write automated tests for Node.js HTTP APIs. |
| Jest | Run automated JavaScript tests and assertions. |
Postman is a popular application used to send HTTP requests and inspect API responses.
First, create a simple Node.js project that we can test using Postman.
npm init -y
npm install express
Create a file named server.js.
const express = require("express");
const app = express();
app.use(express.json());
let students = [
{ id: 1, name: "Rahul", course: "Node.js" },
{ id: 2, name: "Priya", course: "React" }
];
// GET all students
app.get("/students", (req, res) => {
res.json(students);
});
// GET a single student
app.get("/students/:id", (req, res) => {
const id = Number(req.params.id);
const student = students.find(s => s.id === id);
if (!student) {
return res.status(404).json({
message: "Student not found"
});
}
res.json(student);
});
// POST a new student
app.post("/students", (req, res) => {
const { name, course } = req.body;
if (
typeof name !== "string" ||
!name.trim() ||
typeof course !== "string" ||
!course.trim()
) {
return res.status(400).json({
message: "Valid name and course are required"
});
}
const newStudent = {
id: Math.max(0, ...students.map(s => s.id)) + 1,
name: name.trim(),
course: course.trim()
};
students.push(newStudent);
res.status(201).json(newStudent);
});
// PUT - Update student
app.put("/students/:id", (req, res) => {
const id = Number(req.params.id);
const student = students.find(s => s.id === id);
if (!student) {
return res.status(404).json({
message: "Student not found"
});
}
const { name, course } = req.body;
if (
typeof name !== "string" ||
!name.trim() ||
typeof course !== "string" ||
!course.trim()
) {
return res.status(400).json({
message: "Valid name and course are required"
});
}
student.name = name.trim();
student.course = course.trim();
res.json({
message: "Student updated successfully",
student: student
});
});
// DELETE a student
app.delete("/students/:id", (req, res) => {
const id = Number(req.params.id);
const index = students.findIndex(s => s.id === id);
if (index === -1) {
return res.status(404).json({
message: "Student not found"
});
}
students.splice(index, 1);
res.json({
message: "Student deleted successfully"
});
});
app.listen(5000, () => {
console.log("Server running on port 5000");
});
Start your server using:
node server.js
The GET method is used to retrieve data from an API.
Method: GET
URL: http://localhost:5000/students
Steps: Select GET, enter the URL, and click Send.
[
{
"id": 1,
"name": "Rahul",
"course": "Node.js"
},
{
"id": 2,
"name": "Priya",
"course": "React"
}
]
Status Code: 200 OK
You can retrieve a specific student by passing the student ID in the URL.
GET http://localhost:5000/students/1
{
"id": 1,
"name": "Rahul",
"course": "Node.js"
}
Status Code: 200 OK
The POST method is commonly used to create a new resource.
Method: POST
URL: http://localhost:5000/students
Open Body → raw → JSON and enter:
{
"name": "Aman",
"course": "Python"
}
Click Send.
{
"id": 3,
"name": "Aman",
"course": "Python"
}
Status Code: 201 Created
The PUT method is commonly used to replace or update a resource. Our sample API uses PUT to update the student's name and course.
Method: PUT
URL: http://localhost:5000/students/1
Enter this JSON body:
{
"name": "Rahul Kumar",
"course": "Full Stack Development"
}
{
"message": "Student updated successfully",
"student": {
"id": 1,
"name": "Rahul Kumar",
"course": "Full Stack Development"
}
}
Status Code: 200 OK
The DELETE method is used to remove a resource.
Method: DELETE
URL: http://localhost:5000/students/2
{
"message": "Student deleted successfully"
}
Status Code: 200 OK
| Status Code | Meaning | Example |
|---|---|---|
| 200 | OK | Successful GET or PUT request. |
| 201 | Created | A new student was created. |
| 204 | No Content | Successful request with no response body. |
| 400 | Bad Request | Invalid student data. |
| 401 | Unauthorized | Authentication is required or invalid. |
| 403 | Forbidden | The client lacks permission. |
| 404 | Not Found | Student ID does not exist. |
| 500 | Internal Server Error | Unexpected server-side failure. |
A good API should return useful error responses when a request is invalid.
GET http://localhost:5000/students/999
{
"message": "Student not found"
}
Status Code: 404 Not Found
{
"name": "Aman"
}
Send this body in a POST request to /students.
{
"message": "Valid name and course are required"
}
Status Code: 400 Bad Request
You can also test your Node.js API directly from the terminal.
curl http://localhost:5000/students
curl -X POST http://localhost:5000/students \
-H "Content-Type: application/json" \
-d '{"name":"Neha","course":"JavaScript"}'
curl -X DELETE http://localhost:5000/students/1
curl.exe with the appropriate quoting rules,
or enter the command on a single line.
Manual testing is useful while developing an API, but automated testing helps check expected behavior repeatedly.
One popular Node.js testing tool is Supertest, which can send test requests to Express applications.
npm install --save-dev supertest
For automated testing, it is useful to export the Express app separately from the code that starts the server.
const express = require("express");
const app = express();
app.get("/hello", (req, res) => {
res.status(200).json({
message: "Hello API"
});
});
module.exports = app;
const app = require("./app");
app.listen(5000, () => {
console.log("Server running on port 5000");
});
const request = require("supertest");
const assert = require("node:assert/strict");
const test = require("node:test");
const app = require("./app");
test("GET /hello returns the expected response", async () => {
const response = await request(app).get("/hello");
assert.equal(response.status, 200);
assert.deepEqual(response.body, {
message: "Hello API"
});
});
Run the automated test using:
node --test api.test.js
Create a Node.js REST API to manage courses and test its endpoints using Postman.
{
"id": 1,
"course_name": "Node.js",
"duration": "3 Months",
"fee": 5000
}
Bonus Task: Write an automated test that checks whether your GET endpoint returns status code 200.
Select the correct answer for each question.