Automate your requests using Postman Collections – Part #2.

Reading Time: 4 minutes

Previously on the blog, We discussed what is Postman Collection and how to create them. But our main aim is to use these collections to automate our requests, such that, we can dynamically modify:

  • URLs
  • Paths
  • Request parameters
  • And a lot more.

As we referred in the earlier blog,  We first need to understand the reasons for these automations.

  1. Environments: While working with APIs we often test requests for different environments, for example, localhost, stage, production etc.Postman environments let us run a request against different environments without rewriting the requests.
  2. The dependency of one request on another:
    While working with multiple requests under one application that provides responses in such a way that response from endpoint “ABC” depends on the output of the request sent for endpoint “XYZ”. For this, we need dynamic behavior.
  3. Testing:
    Attach test scripts to requests and build integration test suites dynamically.

So, now let’s go through the steps that Postman provides to automate our requests and modify everything dynamically.

Firstly, Let’s have a brief introduction to the flow:

  • We write scripts using javascript.
  • These scripts are used to set and get values of variables being used.
  • These variables are used to modify URLs, paths, parameters etc.
  • We get the response which could be parsed and tested using javascript again.

Scripts in Postman

Postman contains a powerful runtime based on Node.js that allows you to add dynamic behavior to requests and collections. There are two kinds of scripts:

  1. Pre-requisite Scripts: It contains the code that will execute before a request is sent to the server.
  2. Test Scripts: These are executed once we get the response from the server.

Variables in Postman

Similar to other languages, variables are symbols that are used to take different values. Variables allow you to reuse values or modify values according to environments or conditions we put in the scripts.

There are different scopes and types of variables, but mostly we use the global, environment and local variables.

To set a variable in a script, use the methodpm.environment.set() or methodpm.globals.set() depending on the desired scope.

To get a variable’s value in a script, use the methodpm.environment.get() or methodpm.globals.get() depending on the desired scope.

Note: Use the standalone application instead of chrome extension, else the above methods will give exceptions.

To use the variables in requests we use {{variable-name}} format.

Some key points:

  • Environments are basically containing key-value pairs, that defines the setup you want to test.
  • Globals are variables that have the value defined across all the environments. That is the same value for all environments.
  • Environment variables take precedence over global variables (if they have same names).
  • Environment and global variables are always stored as strings. If you’re storing objects/arrays, be sure to JSON.stringify() them before storing, and JSON.parse() them while retrieving.
  • You can create, share, duplicate, export, and delete an environment.

Now, as we have got an idea about scripts and variables, let’s have an example:

Let’s have an example:

Step 1: click on the settings button on the right. Then put the environment name and add the variables. On the same screen, we can add the global variables by clicking on Globals.




Step 2: Provide the dynamic URL with parameters using the variable names given under Environment or Globals.


Step 3: Provide some pre-requisite script to perform some manipulations to the values of the variables according to the environments or some business logic.


Note: This script will execute before the request is sent to the server.

Step 4: Write some test-scripts to do any manipulations needed for further processing or use pm.expecting() method for assertions.


So here was a basic example for dynamically providing the values for requests while using collections. Let us know if something requires more drilling or modifications, because, your views matter to us.

Thanks for giving your time to go through this blog. Keep going through our blogs, because we at Knoldus believe in gaining knowledge by sharing it across.



Written by 

Anmol Mehta is a Software Consultant having experience of more than 1.5 years. A keen programmer who has experience in Scala and Java. He is recognized as a dedicated and determined team player who enjoys working on new technologies. He is a professional and a technology enthusiast. He believes in the approach that “teamwork makes the dream work”. He is a quick and always-on learner who ensures better quality in every increment of his work. He believes in abiding standard coding practices. He always looks after that the team is working in sync with each other both at the technical and managerial level.