REST API · HTTP METHODS · STATUS CODES · COLLECTIONS · ENVIRONMENTS · TESTS · AUTHENTICATION · AUTOMATION
// Complete HTTP Request Structure
// 1. REQUEST LINE
GET /api/users/1 HTTP/1.1
// 2. HEADERS
Host: api.example.com
Content-Type: application/json
Authorization: Bearer eyJ...token
Accept: application/json
// 3. REQUEST BODY (POST/PUT/PATCH only)
{
"name": "John Doe",
"email": "john@example.com",
"age": 25
}
// 4. QUERY PARAMETERS (GET requests)
// URL: /api/users?page=1&limit=10
page=1
limit=10
sort=name
// Complete HTTP Response Structure
// 1. STATUS LINE
HTTP/1.1 200 OK
// 2. RESPONSE HEADERS
Content-Type: application/json
Content-Length: 245
X-Request-ID: abc-123
// 3. RESPONSE BODY (JSON)
{
"id": 1,
"name": "John Doe",
"email": "john@test.com",
"status": "active",
"createdAt": "2025-01-01"
}
| 200 | OK | GET/PUT success, data returned |
| 201 | Created | POST success, new resource created |
| 202 | Accepted | Request accepted, processing later |
| 204 | No Content | Success, no body returned (DELETE) |
| 301 | Moved | Resource permanently moved |
| 304 | Not Modified | Cached version is valid |
| 400 | Bad Request | Invalid request data/syntax |
| 401 | Unauthorized | Authentication required/failed |
| 403 | Forbidden | Authenticated but no permission |
| 404 | Not Found | Resource doesn't exist |
| 405 | Method Not Allowed | HTTP method not supported |
| 409 | Conflict | Duplicate data, state conflict |
| 422 | Unprocessable | Validation failed |
| 429 | Too Many Requests | Rate limit exceeded |
| 500 | Internal Error | Server crashed/bug |
| 502 | Bad Gateway | Invalid response from upstream |
| 503 | Service Unavailable | Server down/overloaded |
| 504 | Gateway Timeout | Upstream server timeout |
| Ctrl + N | New request |
| Ctrl + S | Save request |
| Ctrl + Enter | Send request |
| Ctrl + Alt + C | Open Console |
| Ctrl + / | Search |
// Recommended collection structure
📁 User Management API
📁 Auth
POST Login
POST Logout
POST Refresh Token
📁 Users
GET Get All Users
GET Get User By ID
POST Create User
PUT Update User
DEL Delete User
📁 Negative Tests
GET Invalid User ID
POST Missing Fields
// Set environment variable
pm.environment.set("token", value);
pm.environment.set("userId", "123");
// Get environment variable
const token = pm.environment.get("token");
const url = pm.environment.get("base_url");
// Set global variable
pm.globals.set("apiKey", "abc123");
const key = pm.globals.get("apiKey");
// Set collection variable
pm.collectionVariables.set("key", "val");
// Delete variable
pm.environment.unset("token");
// Use in URL — double curly braces
// URL: {{base_url}}/api/users/{{userId}}
// Add in Params tab OR directly in URL
// URL: /api/users?page=1&limit=10
Key | Value
page | 1
limit | 10
sort | name
filter | active
// Common headers to add
Content-Type | application/json
Authorization | Bearer {{token}}
Accept | application/json
X-API-Key | {{apiKey}}
// Select: Body → raw → JSON
{
"name": "John Doe",
"email": "john@example.com",
"phone": "9876543210",
"role": "student",
"active": true
}
// Select: Body → form-data
Key | Value | Type
username | john | Text
profile_pic | file | File ← click to upload
// Basic test structure
pm.test("Test description", function() {
pm.expect(value).to.equal(expected);
});
// ── STATUS CODE TESTS ──
pm.test("Status is 200", function() {
pm.expect(pm.response.code).to.equal(200);
});
pm.test("Status is 201", () => {
pm.response.to.have.status(201);
});
// Check status in range
pm.test("Success response", () => {
pm.expect(pm.response.code)
.to.be.oneOf([200, 201, 204]);
});
// ── RESPONSE TIME TEST ──
pm.test("Response under 2000ms", () => {
pm.expect(pm.response.responseTime)
.to.be.below(2000);
});
// ── RESPONSE BODY TEXT ──
pm.test("Body has 'success'", () => {
pm.expect(pm.response.text())
.to.include("success");
});
// Parse JSON body first
const json = pm.response.json();
// ── VALUE ASSERTIONS ──
pm.test("Name is John", () => {
pm.expect(json.name).to.equal("John");
});
pm.test("ID is a number", () => {
pm.expect(json.id).to.be.a("number");
});
pm.test("Email is string", () => {
pm.expect(json.email).to.be.a("string");
});
pm.test("Active is true", () => {
pm.expect(json.active).to.be.true;
});
// ── NULL & UNDEFINED ──
pm.expect(json.name).to.not.be.null;
pm.expect(json.id).to.not.be.undefined;
// ── ARRAY TESTS ──
pm.test("Returns array", () => {
pm.expect(json.users).to.be.an("array");
pm.expect(json.users.length).to.be.above(0);
pm.expect(json.users).to.have.lengthOf(10);
});
// ── CONTAINS CHECK ──
pm.expect(json.message)
.to.include("created");
pm.expect(json.roles)
.to.include("admin");
const json = pm.response.json();
// ── CHECK KEY EXISTS ──
pm.test("Has id field", () => {
pm.expect(json).to.have.property("id");
});
pm.test("Has email field", () => {
pm.expect(json).to.have.property("email");
});
// ── NESTED PROPERTIES ──
pm.expect(json.address)
.to.have.property("city");
pm.expect(json.address.city)
.to.equal("Delhi");
// ── MULTIPLE KEYS ──
pm.expect(json).to.include.keys(
["id", "name", "email"]);
// ── HEADER ASSERTIONS ──
pm.test("Content-Type is JSON", () => {
pm.expect(pm.response.headers
.get("Content-Type"))
.to.include("application/json");
});
// ── SCHEMA VALIDATION ──
const schema = {
"type": "object",
"required": ["id", "name"]
};
pm.test("Schema valid", () => {
pm.expect(tv4.validate(json, schema))
.to.be.true;
});
// Pre-request runs BEFORE the request
// Used to: set variables, generate data
// ── SET DYNAMIC DATA ──
const timestamp = new Date().getTime();
pm.environment.set("timestamp", timestamp);
// ── GENERATE RANDOM EMAIL ──
const random = Math.floor(Math.random()*999);
pm.environment.set("email",
`user${random}@test.com`);
// ── CHAIN REQUESTS ──
// Save response for next request
const json = pm.response.json();
pm.environment.set("userId", json.id);
pm.environment.set("token", json.token);
// ── CONSOLE LOG (debug) ──
console.log("Token:", token);
console.log("Response:", json);
// ── RUN NEXT REQUEST ──
// In Tests tab — skip to specific request
postman.setNextRequest("Get User by ID");
// Stop execution
postman.setNextRequest(null);
// Auth Tab → Select Type
// Bearer Token
Type: Bearer Token
Token: {{token}}
// Basic Auth
Type: Basic Auth
Username: admin
Password: admin123
// API Key
Type: API Key
Key: X-API-Key
Value: {{apiKey}}
Add to: Header
// BASE URL: https://api.example.com
// ── CREATE USER (POST) ──
POST /api/users
Body (JSON):
{
"name": "Rahul Verma",
"email": "rahul@qa.com",
"phone": "9876543210"
}
Expected: 201 Created
// ── GET ALL USERS (GET) ──
GET /api/users
Expected: 200 OK + array
// ── GET USER BY ID (GET) ──
GET /api/users/{{userId}}
Expected: 200 OK + single user
// ── UPDATE USER (PUT) ──
PUT /api/users/{{userId}}
Body: { "name": "Rahul K Verma" }
Expected: 200 OK
// ── PARTIAL UPDATE (PATCH) ──
PATCH /api/users/{{userId}}
Body: { "phone": "9999999999" }
Expected: 200 OK
// ── DELETE USER (DELETE) ──
DEL /api/users/{{userId}}
Expected: 200 or 204
// JSON Data Types
{
"string": "Hello World",
"number": 42,
"decimal": 3.14,
"boolean": true,
"nullValue": null,
"array": ["a", "b", "c"],
"nested": {
"city": "Delhi",
"pin": "110001"
}
}
// Real user response JSON
{
"id": 101,
"name": "Khan Sir",
"email": "khan@nextgenqa.com",
"role": "trainer",
"active": true,
"courses": ["Selenium", "Playwright"],
"address": {
"city": "Delhi",
"country": "India"
},
"createdAt": "2025-01-01T10:00:00Z"
}
// Array of users
[
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
// STEP 1: Login request
POST /api/auth/login
Body:
{
"email": "admin@test.com",
"password": "Admin@123"
}
// STEP 2: Save token in Tests tab
const json = pm.response.json();
pm.environment.set("token", json.token);
pm.environment.set("userId", json.user.id);
// STEP 3: Use token in next request
// Auth Tab → Bearer Token → {{token}}
// OR Headers:
Authorization: Bearer {{token}}
// STEP 4: Use userId in URL
GET /api/users/{{userId}}
// PATH parameter — part of URL
GET /api/users/123
// 123 is the path param (user ID)
// QUERY parameter — after ?
GET /api/users?page=1&limit=20
// Both combined
GET /api/users/123/orders?status=paid
// Install Newman
npm install -g newman
// Run a collection
newman run collection.json
// Run with environment
newman run collection.json \
-e environment.json
// Run with HTML report
npm install -g newman-reporter-html
newman run collection.json \
-r html --reporter-html-export report.html
// Run specific folder
newman run collection.json \
--folder "Login Tests"
// Set iterations (run N times)
newman run collection.json -n 3
// Run with data file (CSV)
newman run collection.json \
-d testdata.csv
// Run with delay (ms)
newman run collection.json --delay-request 500