Bonus Tutorial: API Testing with Postman
Bonus Tutorial: API Testing with Postman β Why & How? π
π Why Should You Test APIs Before Frontend Integration?
Before integrating APIs into the frontend, itβs critical to test them independently. Hereβs why:
β
Early Bug Detection β Find API issues before they affect the UI.
β
Faster Debugging β Identify if an issue is in the backend or frontend.
β
Smoother Integration β Ensure API responses match frontend expectations.
β
Security Testing β Test authentication, roles, and protected routes.
β
Automation Ready β Save test cases for future automated testing.
Instead of constantly switching between frontend & backend, Postman lets you test APIs efficiently before connecting them.
π§ Getting Started with Postman
1οΈβ£ Install Postman
- Download & install Postman.
- Open the Postman app and create a free account (optional but useful for saving requests).
π Testing Flask API with Postman
We will test the following API endpoints:
- User Signup
- User Login
- Protected API (JWT Authentication)
- CRUD Operations: Tasks Management
- Role-Based Access Control (Admin & Manager Actions)
1οΈβ£ Testing User Signup API
π Endpoint: POST http://127.0.0.1:5000/signup
πΉ Steps in Postman:
- Open Postman and select
POST
request. - Enter the API URL:
1
http://127.0.0.1:5000/signup
- Go to the Body tab β Select
raw
β ChooseJSON
. - Enter this JSON data:
1 2 3 4 5
{ "username": "testuser", "email": "testuser@example.com", "password": "testpassword" }
- Click Send and check the response.
β Expected Response:
1
2
3
4
{
"username": "testuser",
"email": "testuser@example.com"
}
2οΈβ£ Testing User Login & Generating JWT Token
π Endpoint: POST http://127.0.0.1:5000/login
πΉ Steps in Postman:
- Select
POST
request. - URL:
1
http://127.0.0.1:5000/login
- Body β
raw
β SelectJSON
and enter:1 2 3 4
{ "email": "testuser@example.com", "password": "testpassword" }
- Click Send.
β Expected Response (with JWT Token):
1
2
3
4
{
"msg": "successfully logged in",
"token": "eyJhbGciOiJIUzI1..."
}
β‘ Copy the JWT token β Youβll need it for testing protected APIs.
3οΈβ£ Testing Protected API with JWT Authentication
π Endpoint: GET http://127.0.0.1:5000/hello
πΉ Steps in Postman:
- Select
GET
request. - Enter URL:
1
http://127.0.0.1:5000/hello
- Go to Headers tab.
- Add a new header:
- Key:
Authorization
- Value:
Bearer <JWT-TOKEN>
(Replace<JWT-TOKEN>
with the token copied earlier.)
- Key:
- Click Send.
β Expected Response:
1
2
3
4
{
"msg": "hello world! from flask restful",
"user_name": "testuser"
}
π If you donβt send a valid JWT, it returns 401 Unauthorized.
4οΈβ£ Creating a Task (Admin/Manager Only)
π Endpoint: POST http://127.0.0.1:5000/tasks
πΉ Steps in Postman:
- Select
POST
request. - URL:
1
http://127.0.0.1:5000/tasks
- Headers β Add Authorization Header:
1
Authorization: Bearer <JWT-TOKEN>
- Body β
raw
βJSON
:1 2 3 4 5
{ "title": "Fix Backend Bug", "description": "Resolve API timeout issue", "deadline": "2025-02-20" }
- Click Send.
β Expected Response:
1
2
3
{
"message": "Task created successfully"
}
π If the user isnβt an admin/manager, it returns:
1
2
3
{
"message": "Unauthorized access"
}
5οΈβ£ Fetching All Tasks (Admin/Manager Only)
π Endpoint: GET http://127.0.0.1:5000/tasks
πΉ Steps in Postman:
- Select
GET
request. - URL:
1
http://127.0.0.1:5000/tasks
- Headers β Add Authorization Header:
1
Authorization: Bearer <JWT-TOKEN>
- Click Send.
β Expected Response:
1
2
3
4
5
6
7
8
9
[
{
"id": 1,
"title": "Fix Backend Bug",
"description": "Resolve API timeout issue",
"status": "pending",
"deadline": "2025-02-20"
}
]
6οΈβ£ Assigning a Task to a User (Manager Only)
π Endpoint: PUT http://127.0.0.1:5000/task/1/assign
πΉ Steps in Postman:
- Select
PUT
request. - URL:
1
http://127.0.0.1:5000/task/1/assign
- Headers β Add Authorization Header:
1
Authorization: Bearer <JWT-TOKEN>
- Body β
raw
βJSON
:1 2 3
{ "user_id": 2 }
- Click Send.
β Expected Response:
1
2
3
{
"message": "Task assigned successfully"
}
7οΈβ£ Viewing API Stats (Admin/Manager Only)
π Endpoint: GET http://127.0.0.1:5000/stats
πΉ Steps in Postman:
- Select
GET
request. - URL:
1
http://127.0.0.1:5000/stats
- Headers β Add Authorization Header:
1
Authorization: Bearer <JWT-TOKEN>
- Click Send.
β Expected Response:
1
2
3
4
5
{
"total_users": 5,
"total_tasks": 10,
"completed_tasks": 3
}
Feature | Postman API Testing | Frontend Integration |
---|---|---|
Faster Debugging | β Directly test backend | β Requires UI interactions |
Error Identification | β Isolates API errors | β Hard to debug API issues |
Role-Based Testing | β Easily switch tokens | β Requires logging in/out |
Security Testing | β Test unauthorized access | β Harder to simulate |
Automated Testing | β Save requests & automate | β Requires frontend setup |
No UI Dependency | β Works standalone | β Needs UI components |
πΉ Conclusion: Testing APIs with Postman before frontend integration makes development smoother, debugging faster, and reduces errors before they impact users! ππ₯
π Next Steps
- π₯ Automate Postman tests using Pre-Request Scripts
- π Learn API monitoring with Postman Collections
- π οΈ Integrate APIs with React/Angular Frontend
Happy Testing! ππ―