Dynamic variables. Postman uses the faker library to generate dummy data. You can generate random names, addresses, email addresses, and much more. You can use these pre-defined variables multiple times to return different values per request. Postman is a highly popular complete API development environment having a huge demand for designing, debugging, testing, monitoring, and different other things.The product starts to gain recognition in 2012 when it was firstly announced by its developers.
Thank you for downloading this cheat sheet. This guide refers to the Postman App, not the Chrome extension. Please report any problems with it.
Postman Cheat Sheet is based on the official Postman documentation and own experience.
For a detailed documentation on each feature, check out https://www.getpostman.com/docs.
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).
Microsoft Edge Postman
Learn more about the different variables scopes in this tutorial.
Getting variables in the Request Builder¶
Postman - Only complete API development environment. Swagger UI - A Collection of HTML, Javascript, and CSS assets that dynamically generate beautiful documentation.
Depending on the closest scope:
Syntax: {{myVariable}}
Examples:
Request URL: http://{{domain}}/users/{{userId}}
Headers (key:value): X-{{myHeaderName}}:foo
Request body: {'id':'{{userId}}','name':'JohnDoe'}
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:
- passing data to other requests
Setting
Getting
Alternatively, depending on the scope:
Removing
Remove one variable
Remove ALL global variables (rather unusual)
Collection variables¶
Postman Firefox Alternative
When to use:
- good alternative to global variables or environment variables
- for URLs / authentication credentials if only one environment exists
Setting
Getting
Removing
Environment variables¶
Environment variables are tied to the selected environment. Good alternative to global variables as they have a narrower scope.
When to use:
- storing environment specific information
- URLs, authentication credentials
- passing data to other requests
Setting
Getting
Depending on the closest scope:
Removing
Remove one variable
Remove ALL environment variables
Examples:
** Detecting the environment name **
If you need to know inside scripts which environment is currently active (locahost, production, …) you can use the name property:
Data variables¶
Exist only during the execution of an iteration (created by the Collection Runner or Newman).
When to use:
- when multiple data-sets are needed
Setting
Can only be set from a CSV or a JSON file.
Getting
Depending on the closest scope:
Removing
Can only be removed from within the CSV or JSON file.
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
Getting
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.
Example JSON body:
If you want to use dynamic variables in scripts, you can use the replaceIn starting with Postman v7.6.0.
For more details please see the section dedicated to Dynamic variables
Logging / Debugging variables¶
Open Postman Console and use console.log in your test or pre-request script.
Example:
Assertions¶
Note: You need to add any of the assertions inside a pm.test
callback.
Example:
Status code¶
Check if status code is 200:
Checking multiple status codes:
Response time¶
Response time below 100ms:
Headers¶
Header exists:
Header has value:
Cookies¶
Cookie exists:
Cookie has value:
Body¶
Any content type / HTML responses
Exact body match:
Partial body match / body contains:
JSON responses
Parse body (need for all assertions):
Simple value check:
Nested value check:
XML responses
Convert XML body to JSON:
Note: see assertions for JSON responses.
Skipping tests¶
You can use pm.test.skip to skip a test. Skipped tests will be displayed in reports.
Simple example
Conditional skip
Failing tests¶
Postman Firefox
You can fail a test from the scripts without writing an assertion:
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:
Full-option HTTP POST request with JSON body:
Form-data POST request (Postman will add the multipart/form-data header):
Sending a file with form-data POST request
Postman Browser Plugin
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.
Get Current UTC time in pre-request script
Workflows¶
Rest Client Firefox
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.
Set which will be the next request to be executed
postman.setNextRequest(“Requestname');
Stop executing requests / stop the collection run
postman.setNextRequest(null);