🚀 Postman & API Testing Masterclass
1️⃣ What is an API & REST Architecture
Definition: API (Application Programming Interface) do software systems ke beech ek intermediate communication bridge hoti hai jo unhe standard data formats (jaise JSON ya XML) me communicate karne deti hai.
- Client-Server Architecture: UI aur Database logic ek-dusre se completely separated hote hain.
- Stateless: Har request independent hoti hai; server kisi client session state ko memory me hold nahi karta.
- Cacheable: Responses explicit hote hain ki browser ya client unhe cache kar sakta hai ya nahi.
- Uniform Interface: Consistent endpoints (e.g.
/api/v1/users). - Layered System: Client direct server se connect hai ya proxy/CDN se, fark nahi padta.
2️⃣ HTTP Methods, Headers & Parameters
| HTTP Verb | CRUD Action | Idempotent? | Has Body? | Standard Role |
|---|---|---|---|---|
| GET | Read | Yes | No | Server se records fetch karna (e.g. /users?page=2). |
| POST | Create | No | Yes (JSON) | Naya record create karna (e.g. User registration). |
| PUT | Update (Full) | Yes | Yes (JSON) | Pure resource ko naye payload se completely replace karna. |
| PATCH | Update (Partial) | No | Yes (JSON) | Resource ke sirf specific field (e.g. only email) ko update karna. |
| DELETE | Delete | Yes | Optional | Resource ko database se delete karna. |
• Path Param: Specific resource identify karta hai (e.g.
/users/42 → User ID 42).• Query Param: List ko filter ya sort karta hai (e.g.
/users?role=admin&sort=asc).
3️⃣ HTTP Status Code Taxonomy
| Range | Meaning | Common Examples & Roles |
|---|---|---|
| 2xx | Success | 200 OK (Success), 201 Created (Post success), 204 No Content (Delete success) |
| 3xx | Redirection | 301 Moved Permanently, 304 Not Modified (Cached asset) |
| 4xx | Client Error | 400 Bad Request, 401 Unauthorized (No token), 403 Forbidden (No permission), 404 Not Found |
| 5xx | Server Error | 500 Internal Server Error (Backend crash), 502 Bad Gateway, 503 Service Unavailable |
4️⃣ Postman Workspace & Request Execution
Postman ek complete API client platform hai jo manual testing, automated assertion aur documentation generation automate karta hai.
5️⃣ Environment Variables & Token Chaining
Dynamic authentication workflows me Login API se token extract karke future requests me pass kiya jata hai:
// Tests tab of "POST /auth/login":
// 1. Extract token from response JSON
const responseData = pm.response.json();
const authToken = responseData.token;
// 2. Set dynamically in environment
pm.environment.set("jwt_token", authToken);
// Usage in subsequent requests:
// Headers: Authorization → Bearer {{jwt_token}}
// Base URL: {{base_url}}/api/v1/orders
6️⃣ Writing Automated Test Scripts with Chai JS
Postman ke "Tests" tab me JavaScript likhkar API response ko validate kiya jata hai:
// 1. Validate Status Code is 200 OK
pm.test("Status code is 200 OK", function () {
pm.response.to.have.status(200);
});
// 2. Validate Response Time is acceptable
pm.test("Response time is less than 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
// 3. Validate JSON Body Schema & Values
pm.test("User object contains email", function () {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property("email");
pm.expect(jsonData.email).to.eql("rahul@example.com");
});
7️⃣ Newman CLI & CI/CD Pipelines
Postman collections ko command-line aur CI/CD pipelines (Jenkins/GitHub Actions) me headless run karne ke liye Newman use hota hai:
# 1. Install Newman globally
npm install -g newman newman-reporter-htmlextra
# 2. Run Collection with Environment file
newman run User_API_Collection.json -e Staging_Environment.json
# 3. Generate Rich HTML Test Report
newman run User_API_Collection.json -e Staging_Environment.json -r htmlextra