Postman Cheatsheet
Postman Cheatsheet
This guide refers to the Postman App. Postman Cheat Sheet is based on the official
Postman documentation and own experience.
Variables
All variables can be manually set using the Postman GUI and are scoped.
The code snippets can be used for working with variables in scripts (pre-request,
tests).
Syntax: {{myVariable}}
Examples:
Global variables
General purpose variables, ideal for quick results and prototyping.
Please consider using one of the more specialized variables below. Delete variables
once they are no longer needed.
When to use:
Setting
pm.globals.set('myVariable', MY_VALUE);
Getting
pm.globals.get('myVariable');
Alternatively, depending on the scope:
pm.variables.get('myVariable');
Removing
Collection variables
When to use:
Setting
pm.collectionVariables.set('myVariable', MY_VALUE);
Getting
pm.collectionVariables.get('myVariable');
Removing
pm.collectionVariables.unset('myVariable');
Environment variables
Environment variables are tied to the selected environment. Good alternative to
global variables as they have a narrower scope.
When to use:
Setting
pm.environment.set('myVariable', MY_VALUE);
Getting
pm.environment.get('myVariable');
Removing
Examples:
pm.environment.set('name', 'John Doe');
console.log(pm.environment.get('name'));
console.log(pm.variables.get('name'));
If you need to know inside scripts which environment is currently active (locahost,
production, …) you can use the name property:
pm.environment.name
Data variables
Exist only during the execution of an iteration (created by the Collection Runner or
Newman).
When to use:
Setting
Getting
pm.iterationData.get('myVariable);
Local variables
Local variables are only available withing the request that has set them or when using
Newman / Collection runner during the entire execution.
When to use:
• whenever you would like to override all other variable scopes — for whatever
reason. Not sure though then this is needed.
Setting
pm.variables.set('myVariable', MY_VALUE);
Getting
pm.variables.get('myVariable', MY_VALUE);
Removing
Local variables are automatically removed once the tests have been executed.
Dynamic variables
All dynamic variables can be combined with strings, in order to generate dynamic /
unique data.
If you want to use dynamic variables in scripts, you can use the replaceIn starting with
Postman v7.6.0.
pm.variables.replaceIn('{{$randomFirstName}}'); // returns a String
For more details please see the section dedicated to Dynamic variables
Example:
var myVar = pm.globals.get("myVar");
console.log(myVar);
Assertions
Note: You need to add any of the assertions inside a pm.test callback.
Example:
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
Status code
Check if status code is 200:
pm.response.to.have.status(200);
Response time
Response time below 100ms:
pm.expect(pm.response.responseTime).to.be.below(9);
Headers
Header exists:
pm.response.to.have.header('X-Cache');
Cookies
Cookie exists:
pm.expect(pm.cookies.has('sessionId')).to.be.true;
Body
Any content type / HTML responses
JSON responses
XML responses
Skipping tests
You can use pm.test.skip to skip a test. Skipped tests will be displayed in reports.
Simple example
pm.test.skip("Status code is 200", () => {
pm.response.to.have.status(200);
});
Conditional skip
const shouldBeSkipped = true; // some condition
Failing tests
You can fail a test from the scripts without writing an assertion:
pm.expect.fail('This failed because ...');
Postman Sandbox
pm
this is the object containing the script that is running, can access variables and has
access to a read-only copy of the request or response.
pm.sendRequest
Allows to send simple HTTP(S) GET requests from tests and pre-request scripts.
Example:
pm.sendRequest('https://httpbin.org/get', (error, response) => {
if (error) throw new Error(error);
console.log(response.json());
});
const options = {
method: 'POST',
url: 'https://httpbin.org/post',
header: 'X-Foo:foo',
body: {
mode: 'raw',
raw: JSON.stringify(payload)
}
};
pm.sendRequest(options, (error, response) => {
if (error) throw new Error(error);
console.log(response.json());
});
Due to security precautions, it is not possible to upload a file from a script using
pm.sendRequest. You cannot read or write files from scripts.
Postman Echo
Helper API for testing requests. Read more at: https://docs.postman-echo.com.
Workflows
Only work with automated collection runs such as with the Collection Runner or
Newman. It will NOT have any effect when using inside the Postman App.
Additionaly it is important to note that this will only affect the next request being
executed. Even if you put this inside the pre-request script, it will NOT skip the
current request.
postman.setNextRequest(“Request name");
postman.setNextRequest(null);