Ultimate Postman Cheat Sheet



Ultimate Postman Cheat Sheet

A reference for API testing, automation scripts, and Newman CLI.

1. Variables & Scope

Access and modify variables within the "Pre-request Script" or "Tests" tabs.

Get Variables

pm.environment.get("variable_key"); pm.globals.get("variable_key"); pm.variables.get("variable_key"); // Checks all scopes

Set Variables

pm.environment.set("key", "value"); pm.globals.set("key", "value"); pm.collectionVariables.set("key", "value");

Remove Variables

pm.environment.unset("key"); pm.globals.unset("key");

2. Scripting & Parsing

Handling responses and debugging logic.

Parse JSON Response

Convert the response body into a JavaScript object.

var jsonData = pm.response.json(); console.log(jsonData.token);

Parse XML Response

var xmlData = xml2Json(responseBody);

Logging

Open the Postman Console (Ctrl+Alt+C) to view these logs.

console.log("Debug message"); console.warn("Warning message"); console.error("Error message");

3. Assertions (pm.test)

Writing tests to validate API behavior.

Status Code Check

pm.test("Status is 200 OK", function () { pm.response.to.have.status(200); });

Response Time Check

pm.test("Response time is < 500ms", function () { pm.expect(pm.response.responseTime).to.be.below(500); });

Check JSON Body Content

pm.test("Check if user is Admin", function () { var jsonData = pm.response.json(); pm.expect(jsonData.role).to.eql("admin"); pm.expect(jsonData.id).to.be.a("number"); });

Check Headers

pm.test("Content-Type is JSON", function () { pm.response.to.have.header("Content-Type", "application/json"); });

4. Dynamic Variables

Postman can generate random data inside the Request Builder using {{...}} syntax.

VariableOutput
{{$guid}}v4 unique identifier
{{$timestamp}}Current UNIX timestamp
{{$randomInt}}Random integer (0-1000)
{{$randomEmail}}Random fake email
{{$randomFirstName}}Random first name
{{$randomCity}}Random city name

5. Workflows (Chaining Requests)

Control the order of execution in the Collection Runner.

Set Next Request

Jump to a specific request by name.

pm.execution.setNextRequest("Login API");

Stop Execution

Stop the runner immediately.

pm.execution.setNextRequest(null);
Use Case:

In a "Login" request test, check if login failed. If yes, use setNextRequest(null) to stop the tests so you don't run 50 failed requests.

6. Newman CLI

Run Postman collections from the command line (CI/CD pipelines).

Installation

npm install -g newman

Run Collection

newman run collection.json

Run with Environment

Load environment variables file.

newman run collection.json -e environment.json

Generate Report (HTML)

Requires newman-reporter-html.

newman run collection.json -r html

Bail on Failure

Stop the pipeline immediately if a test fails.

newman run collection.json --bail