Postman Quick Reference Guide Documentation

Postman Quick Reference Guide Documentation

Release Version 1.8.0 - August 2021

Valentin Despa

Aug 16, 2021

Contents:

1 Cheatsheet

1

1.1 Postman Cheatsheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 Dynamic variables

9

2.1 Dynamic variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3 Simple solutions to common problems

15

3.1 Request creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

3.2 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

3.3 Schema validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

3.4 JavaScript libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3.5 Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

3.6 Newman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

4 Online course

31

4.1 Contributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

4.2 Postman online course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

i

ii

1 CHAPTER

Cheatsheet

1.1 Postman Cheatsheet

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 .

1.1.1 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). Learn more about the different variables scopes in this tutorial. Getting variables in the Request Builder 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": "John Doe"} 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:

1

Postman Quick Reference Guide Documentation, Release Version 1.8.0 - August 2021

? passing data to other requests Setting pm.globals.set('myVariable', MY_VALUE); Getting pm.globals.get('myVariable'); Alternatively, depending on the scope: pm.variables.get('myVariable'); Removing Remove one variable pm.globals.unset('myVariable'); Remove ALL global variables (rather unusual) pm.globals.clear();

Collection variables When to use:

? good alternative to global variables or environment variables ? for URLs / authentication credentials if only one environment exists 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:

? storing environment specific information ? URLs, authentication credentials ? passing data to other requests Setting pm.environment.set('myVariable', MY_VALUE); Getting

2

Chapter 1. Cheatsheet

Postman Quick Reference Guide Documentation, Release Version 1.8.0 - August 2021

pm.environment.get('myVariable');

Depending on the closest scope: pm.variables.get('myVariable');

Removing Remove one variable pm.environment.unset("myVariable");

Remove ALL environment variables pm.environment.clear();

Examples: pm.environment.set('name', 'John Doe'); console.log(pm.environment.get('name')); console.log(pm.variables.get('name'));

** Detecting the environment 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:

? when multiple data-sets are needed Setting Can only be set from a CSV or a JSON file. Getting pm.iterationData.get('myVariable);

Depending on the closest scope: pm.variables.get('myVariable');

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.

1.1. Postman Cheatsheet

3

Postman Quick Reference Guide Documentation, Release Version 1.8.0 - August 2021

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. Example JSON body: {"name": "John Doe", "email": "john.doe.{{$timestamp}}@"} 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

Logging / Debugging variables Open Postman Console and use console.log in your test or pre-request script. Example: var myVar = pm.globals.get("myVar"); console.log(myVar);

1.1.2 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); Checking multiple status codes: pm.expect(pm.response.code).to.be.oneOf([201,202]);

4

Chapter 1. Cheatsheet

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download