Testing RESTful APIs with Postman: A Practical Example
Postman simplifies API testing by allowing users to construct, send, and validate HTTP requests against REST endpoints. Consider a typical user management API that supports standard CRUD operations.
Core Endpoints
GET /api/users– Retrieve all usersPOST /api/users– Create a new userGET /api/users/:id– Fetch a specific userPUT /api/users/:id– Update a user’s detailsDELETE /api/users/:id– Remove a user
Sending Requests
Retrieve All Users
Set the method to GET and enter the full URL (e.g., https://api.example.com/api/users). Click Send to view the JSON response, status code, and timing metrics.
Create a New User
Use the POST method with the same base URL. In the Body tab, select raw and choose JSON. Provide a payload like:
{
"name": "Alice Smith",
"email": "alice.smith@example.com"
}
After sending, verify the response includes the created user data and a 201 Created status.
Fetch a Specific User
Issue a GET request to https://api.example.com/api/users/5, replacing 5 with a valid ID. Confirm the returned object matches expected attributes.
Update a User
Select PUT, target the user’s endpoint (e.g., /api/users/5), and include an updated JSON body:
{
"name": "Alice Johnson",
"email": "alice.johnson@example.com"
}
A successful update typically returns 200 OK with the modified resource.
Delete a User
Send a DELETE request to the user’s endpoint. A 204 No Content or 200 OK usually indicates successful deletion.
Writing Automated Tests
In the Tests tab, add JavaScript assertions to validate behavior:
pm.test("Response status is 200", () => {
pm.response.to.have.status(200);
});
pm.test("User email matches expected value", () => {
const res = pm.response.json();
pm.expect(res.email).to.eql("alice.johnson@example.com");
});
Test results appear in the Test Results panel after each request.
Organizing Workflows
Group related requests into a Collection via the Save button for reuse. Lveerage Environments to manage variables like {{base_url}}, enabling seamless switching between development, staging, and production setups without modifying individual requests.