Table of contents
1.
Introduction📪👮
2.
Variables in Postman 🔠
2.1.
Global Variables in Postman🌍🔠
2.2.
Collections Variables in Postman🤓
2.3.
Environment variables in Postman
2.4.
Data Variables in Postman📁
2.5.
Debugging Variables in Postman🐛
3.
Assertions in Postman 🤖
3.1.
Skipping Test in Assertions🧪
3.2.
Failing Test in Assertions🧪
4.
Sandbox in Postman ⏳📦
5.
Postman Echo🥏
6.
Workflows in Postman 🧑‍💼
7.
Frequently Asked Questions
7.1.
What is the Payload in Postman?
7.2.
Can we declare the same names in Postman for global variables?
7.3.
What is a collection in Postman?
8.
Conclusion
Last Updated: Mar 27, 2024

Take a Look at the Cheatsheet of Postman Commands

Author Ayush Mishra
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction📪👮

Technically, API is an abbreviation for Application Programming Interface. Most large corporations have created APIs for their customers or for internal use at some point.

Postman is an Application Programming Interface (API) testing tool created in 2012 by software developer and entrepreneur Abhinav Asthana to simplify API development and testing.

Postman cheatsheet

 In this blog, we will discuss the Cheatsheet of Postman Commands in detail. Let's start going!

Variables in Postman 🔠

Variables, like any other programming language, are simply placeholders for some value or the outcome of some expressions. The values can be stored in variables and used throughout requests, environments, collections, and scripts.

Syntax of declaring variables in Postman is:

var x = “Hello Postman”
In this blog, we will quickly revise postman command

Global Variables in Postman🌍🔠

In this part of "Cheatsheet of Postman Commands," we will discuss Global variables in Postman.

Global variables in Postman can be used in any environment and can be used to execute any request.

It can be used when data transfer to other requests is required.

Syntax of the setting of a global variable:

pm.globals.set('VariableName', Roll_No);

 

Syntax of the getting of a global variable:

pm.globals.get('VariableName');

Collections Variables in Postman🤓

In this part of  "Cheatsheet of Postman Commands," we will discuss Collections variables in Postman. 

Collections variables are environment-independent and available for all requests in a collection. It do not change while a collection or request is being executed within the given collection. It could essentially be retrieved but not modified during request execution.

Uses of Collections variables are:

  • Firstly, a good substitute for global or environmental variables.
  • Secondly, for URLs / authentication credentials, if there is just one environment.
     

Syntax of the setting of a collection variable:

pm.collectionVariables.set('VariableName', Roll_No); 

 

Syntax of the getting of a collection variable:

pm.collectionVariables.get('VariableName');

Environment variables in Postman

In this part of "Cheatsheet of Postman Commands," we will discuss Environment variables in Postman. 

Environment variables are the most common type of variable in Postman. They are linked to a specific environment used to carry out the request. They have a greater scope than Global variables but a narrower scope than Collection variables.

Uses of Environment variables are:

  • Firstly, it is used to save information about the environment.
  • Secondly, it is used for URLs, authentication credentials, and data forwarding to other requests.
     

Syntax of the setting of an Environment variable:

pm.environment.set('VariableName', Roll_No); 

 

Syntax of the getting of an Environment variable:

pm.environment.get('VariableName'); 

Data Variables in Postman📁

In this part of "Cheatsheet of Postman Commands," we will discuss Data variables in Postman. 

Data variables in Postman appear during request execution via the collection runner. It is vital to note that the source of Data variables is the user-supplied data file in JSON or CSV format. It can only be fetched but not updated/modified or added during request execution.

It is used when multiple datasets are required. It can only be set from a CSV or JSON file and can only be removed from within the CSV or JSON file.

Syntax of the getting of a Data variable:

pm.iterationData.get('VariableName’);

Debugging Variables in Postman🐛

To debug variables in Postman, use console.log in your program.
 

In this context, we will debug the bugs in variable.

Example of debugging variables in Postman

var VarName = pm.globals.get("VariableName");
console.log(VarName);

Assertions in Postman 🤖

In this part of "Cheatsheet of Postman Commands," we will discuss assertions of command in Postman.

In any programming language, the assertion determines whether the supplied forecast is true or false. A forecast is a Boolean expression that returns either true or false. The assertion is nothing more than code that can be used to check or analyze the server answer.

Example of Assertions:

pm.test("Test Name", function ()
 {
var Data_of_json = pm.response.json(); 
pm.expect(Data_of_json.value).to.eql(200);
 });

 

Syntax to check the status of the request is 200:

pm.response.to.have.status(200); 

 

Syntax to check the status of the request of multiple codes are:

pm.expect(pm.response.code).to.be.oneOf([201,202]); 

 

Syntax to check the response time:

pm.expect(pm.response.responseTime).to.be.below(9);

 

Syntax to check whether header exists or not:

pm.response.to.have.header('X-Cache'); 

 

Syntax to check header value:

pm.expect(pm.response.headers.get('X-Cache')).to.eql('HIT'); 

 

Syntax to check whether cookies exist or not:

pm.expect(pm.cookies.has('sessionId')).to.be.true; 

 

Syntax to check cookie has value:

pm.expect(pm.cookies.get('sessionId')).to.eql('ad3se3ss8sg7sg3');


Syntax to check whether it is an exact body or not:

pm.response.to.have.body("OK");
pm.response.to.have.body('{"success"=true}'); 

 

Syntax to check partial body:

pm.expect(pm.response.text()).to.include('Order delivered.'); 

 

Syntax to check whether the parsed body is JSON or not:

const response = pm.response.json(); 

 

Syntax to check simple value:



pm.expect(response.indexno).to.eql(50); 
pm.expect(response.roll_no).to.eql('20202303');

 

Syntax to check nested value:

pm.expect(response.products.0.category).to.eql('Biscuit'); 
Skipping and failing testing in assertions

Skipping Test in Assertions🧪

A skipping test is used to skip the running test in a particular object. To perform a skipping test, use the pm.test.skip skip a test. It will be displayed in reports.

Simple Skipping test example:

pm.test.skip("Status code is 202", () => {
pm.response.to.have.status(202);
 });

 

Conditional Skipping test example:

const shouldBeSkipped = true;      //  condition
 (shouldBeSkipped ? pm.test.skip : pm.test) ("Status code is 202", () => { pm.response.to.have.status(202); 
});

Failing Test in Assertions🧪

A failing test is used to fail a test without writing an assertion. Syntax of failing test is:

pm.expect.fail('This test failed ...'); 

Sandbox in Postman ⏳📦

In this part of “Cheatsheet of Postman Commands,” we will discuss Sandbox in Postman.

The Postman Sandbox is a JavaScript execution environment that you can use while creating pre-request scripts and testing requests both in Postman and Newman. This sandbox executes any code you write in the pre-request/test script section.

pm API in Postman Sandbo is the object that contains the running script, has access to variables, and has a read-only copy of the request or response.

pm.sendRequest allows simple HTTP(S) GET requests to be sent from tests and pre-request scripts. An example to send HTTP(S) GET requests is:

pm.sendRequest('https://httpbin.org/get', (error, response) => { 
if (error) throw new Error(error); 
console.log(response.json());
 });

 

An example of getting an HTTP POST request with a JSON body is:

const payload = { name: 'Mick', age: 12};
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());
 });  

 

An example to get the data of form request is:

const options = {
'method': 'POST', 
'url': 'https://httpbin.org/post', 
'body': { 
'mode': 'formdata', 
'formdata': [ {'key':'foo', 'value':'bar'}, {'key':'bar', 'value':'foo'} ]
 } 
}; 
pm.sendRequest(options, (error, response) => { 
if (error) throw new Error(error); 
console.log(response.json());
 });

Postman Echo🥏

In this part of “Cheatsheet of Postman Commands,” we will discuss Postman Echo.

Postman Echo is a tool for testing REST clients and making sample API requests. It supports GET, POST, PUT, multiple authentication techniques, and other utility endpoints.

An example to get the current UTC time in the pre-request script is:

pm.sendRequest('https://postman-echo.com/time/now', function (err, res) { 
if (err)
 { 
console.log(err);
 }
else 
 {
 var cuTime = res.stream.toString(); 
 console.log(cuTime); 
 pm.environment.set("currentTime", cuTime); 
} 
}); 

Workflows in Postman 🧑‍💼

In this last part of “Cheatsheet of Postman Commands,” we will discuss Workflow in Postman.

Postman Workflow is the sequence of request execution within a collection. When you run the collection directly, the requests are either randomly or serially executed. When used within the Postman App, it has no effect.

Postman Workflow never affects the current requests inside the pre-request script, but it affects the next requests being executed.

Syntax to set which request will be executed by Postman next:

postman.setNextRequest(“Name of the Request");

 

Syntax to stop executing request is:

postman.setNextRequest(null);

Frequently Asked Questions

What is the Payload in Postman?

The body of your request and response messages is the Payload of an API Module. It contains the data you send to the server when you make an API request. Payloads can be sent and received in various formats, such as JSON or XML.

Can we declare the same names in Postman for global variables?

Global variables have a global scope that is limited to the workspace. As a result, variables with global scope cannot have the same names. Local variables can have the same names, but they must be part of different variables.

What is a collection in Postman?

A collection in Postman is helpful for grouping related requests. It aids in the orderly organization of requests into folders. An infinite number of folders can be created inside the folders.

Conclusion

Congratulations on finishing the blog! We have discussed the Cheatsheet of Postman Commands. We further look at the commands to get, set, and remove the variables in Postman and commands in assertions, sandbox, echo, and workflows in Postman.

If you enjoyed this post, you should read more about the Postman. Some of them are:-

Please refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Please do upvote our blogs if you find them helpful and informative!

Happy learning!

Live masterclass