Open In App

How To Run Postman Collection Again, Passing A Different Value Into Environment Variable?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Postman is an application that helps users to test their application’s APIs using a graphical user interface. It allows the user to specify the details of various types of API requests and test their functionality. It also provides pre-request scripts to run a few tests or perform some operations before making these API calls and tests executed after an API call.

Pre-requisites

This article requires you to have Postman installed on your machine or at least you can use a web extension to follow along.

The following is what it looks like. There are a couple of things we need to know before we proceed further.

image-(2)

Postman User Interface

Key Terminologies

  1. Collections: As the name says it is a group of multiple HTTP requests; logically all the requests of one entity are put under a folder and all these folders are created inside a collection. A collection of APIs represents an entire application’s or service’s APIs. All the requests can be written inside one collection too without any folders. It is usually the developer’s and the team’s choice of how they want to organize their APIs.
  2. Workspaces: Workspaces in Postman allows developers around the world to work on a common set of API development and testing. It provides a shared space where team members can create, organize, and collaborate on API-related resources such as collections, environments, and documentation.
  3. Requests: A request refers to an HTTP request that you send to a server to test its API; you can send various HTTP request types, including GET, POST, PUT, PATCH, DELETE, and more. The request type determines the action you want to perform on the server.
  4. Pre-request Scripts: You can include pre-request scripts that are executed before the request is sent. These scripts allow you to dynamically modify request parameters, headers, or bodies based on certain conditions.
  5. Tests: Postman allows you to write tests to validate the response received from the server. These tests can be written using JavaScript and are executed after the request is completed.
  6. Environments: These are used to manage different sets of values or configurations that can be dynamically applied to API requests during testing or development. Environments in Postman allow you to create variations of variables such as API URLs, authentication tokens, and other parameters so that you can seamlessly switch between different setups without modifying individual requests; which is our topic for the day.

Working with Environments in Postman

Let us quickly create a sample collection using the automation options available. Click on the + button under the Collections section. If you hover on this button you should see the “Create a new collection” label.

Then, choose REST API template under Create from the template section. The step-by-step flow is shown in the image below:

file

Creating a collection

This is how it looks if the above step is completed successfully. Now, what you are seeing is the group of four basic HTTP requests.

image-(13)

Basic HTTP requests created by Automation

If you take a close look at the right top corner of the image shown below. You will see “No Environment” That’s because we haven’t configured any environment as of now. Here are the following steps you need to perform to achieve today’s target.

Creating Environments

You can create environments in Postman by clicking on the “report with eye” icon in the top right corner(just beside No Environment on the right). Here, you can define a new environment and set key-value pairs for variables.

The following view is what you see when you click on the specified icon.

file

Create Environment variable

Defining Variables

Enter the name of the environment and add a sample variable. I am adding SERVER_URL, which I want to use to hit the APIs of my application in LOCAL. Hence I name my Environment LOCAL as shown below.

image-(9)

The first Environmental variable

Switching Environments

Now, if you click on the environments on the top right you will see local in the drop down. Click on LOCAL. Now you should be able to use the SERVER_URL variable that you created just a while ago.

file

LOCAL environment creation success

Variables in Requests

When creating or editing an API request, you can reference variables using double curly braces {{}} as shown below I am using SERVER_URL enclosed in {{}}. If you observe on the right-hand side, the value that I have configured is taken while making the request.

image-(11)

Request using Environment – LOCAL

Now, switch to No Environment on the right top and then proceed with new Environment creation steps. You can call it DEV indicating the DEV environment and you can add the same environment variables label but provide a desired different value as I have done here. This approach allows you to pass different values to the same ENV variable just by changing the environment from the right top drop-down.

image-(12)

Request using Environment – DEV

Now you can click on three dots just beside your collection name and select the Run Collection option, followed by a view to allow you to choose how you want to run the collection, manually or schedule it for some other time.

Troubleshooting Environments

  1. Verify that the requests in your collection are configured correctly, including the use of environment variables in URLs, headers, and request bodies double-check the authentication configurations.
  2. If you suspect that there might be corruption or unexpected behavior in your environment, you can try resetting the environment by closing and reopening Postman or restarting the Postman app.
  3. Visit the Postman Community forums and regularly refer to the Postman documentation for the latest information on the tool and its features.

Conclusion

Postman provides flexibility in API testing through their Environments feature. It helps users test the APIs for different environments like DEV, LOCAL, and QA without having to modify the request but with a simple drop-down click. It not only enhances the overall testing process but also results in faster overall API development.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads