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:
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.
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.
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.
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.
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.
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.
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.
How To Run Postman Collection Again, Passing A Different Value Into Environment Variable?
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.