Variables in POSTMAN

Variables are used to store a value in POSTMAN most of the time we will store part of the URL in a variable.

Using this concept of storing a part of the URL in a variable and using it in multiple places can save the time of typing a lengthy URL. Also, this technique can very helpful when you want to make changes in the URL.

In this section, we shall learn about creating a variable, use a variable, and refer a variable.

For example,

URL 1 : https://reqres.in/api/users/2

URL 2 : https://reqres.in/api/users?/page=2

Two requests which are inside the collection- Collection01 have the first website link in common; just the request end-points are different. Therefore, the first part can be stored in a variable so that, the variable can be used when the website URL is required.

To open the variable section and click the three dots beside the collection name, and then a dropdown list will appear. Select Edit from the list.

postman-edit-variables

After you select Edit, you should see as below:

postman-variables

Now, click on the variable tab and you will be asked to enter the key, which is the variable name(anything you want). Now, copy the first part of the URL which is common to both of the requests.

In our case, one URL is: https://reqres.in/api/users/2

And another URL is: https://reqres.in/api/users?/page=2

You can see that the URL part https://reqres.in is common to both the above URLs. Therefore, we shall try to store this part of the URL in a variable, and try substituting it inside the POSTMAN software and see whether it works.

To do so, click on the Variables tab and then provide a name to the key(url) and then edit Initial and Current value. And then select the Update button.

postman-variable-edit

After you have named and updated your variable, go to the URL bar and replace the part of the URL with the variable you declared.

In our case, the variable is the url and the syntax contains variable between a pair of braces {{ }}

postman-variable-replacement

After successful replacement of the variable, you are ready to go. Now, send the request again and it should work the same way it worked earlier.

postman-send-button-check

Check the output now:

postman-variable-replacement-working

Variables in the Environment

Variables can be set in POSTMAN Environment directly. Environment variables will be useful when your application has different environments, Typically software will have below environments:

  • Development
  • QA
  • Production
Environment Variable:

To create Environment Variable, go to Manage Environment.

postman-manage-environment

Then click into the Add button.

postman-variable-add-button

After you click Add you will get an area to input the variable name, value, etc.

postman-variable-adding

In my case my VARIABLE is url01. The INITIAL VALUE is http://reqres.in/ And the CURRENT VALUE will automatically take the same as that of the INITIAL VALUE.

Now, before adding the variable and values, enter a suitable environment name and then click Add.

postman-environment-naming

Now, when you add your variable, it is added successfully. Now, go to the request URL bar and replace the part of the URL with the variable name. In my case, the URL part I want to replace is: http://reqres.in/. The variable which must be placed inside a double brace. For example, {{url01}} in the URL.

postman-variables-replacement

And then select the Environment as new-environment.postman-send-button-with-environment

Now, the request should work well and the response should be visible below in the software.

Response from the server :

{
    "page": 1,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        {
            "id": 1,
            "email": "[email protected]",
            "first_name": "George",
            "last_name": "Bluth",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
        }
    ]
}

So, we have seen that how we can set variables in the environment and still the URL runs a similar way.

Appropriate environment(in which the variables are set) should be selected before targetting to send the request. An irrelevant environment will produce an error.

Global Variables

In the previous section we have seen how can we set an environment variable and when the request has to be run, we select the required environment and then send the request to get the response.

However, when variables are set in the environment level, then the variable can only be used when the request is within the environment. But it is also possible to set variables globally, where the variable can be used in any of the environments.

Global Variables are used when we create different environments with different requests inside them. But all the request contains a part of the URL which is common to all.

To create Global variables, go to Manage Environment and click Globals.

postman-global-varable-management-icon

postman-global-variable-option

Then you will get an area to create global variables and set value for them.

I shall create a variable for the URL part- https://reqres.in and will name the variable as cvalue(which I indicate the common value, as this URL part is going to be used in different requests, you can use any name you want).

postman-global-variable-creation

Now, save it.

postman-save-global-variable

Now, create any request which requires the URL part- https://reqres.in And then append the required end-point and send the request.

For example, I shall visit the link: https://reqres.in and then select a GET request. I shall Select the List User, which is GET type and copy the request end-point.

postman-get-method-example

Now, go to the URL bar of the request and use the variable cvalue in place of https://reqres.in and then append the request end-point.

postman-set-global-variable

Now, select any environment of your choice(does not matter which one you choose), the request will work for all the environment. This is the use of a Global Variable.

Let us select any environment and send the request. First, I shall select the environment myenv.

postman-select-any-environment-one

Then send the request.

Secondly, I select a different environment called: new-environment.

postman-select-any-environment-two

Now again send the request.

In both cases, I have got the same response. It means, that the global variable is in action and is working the same for different environments.

Response :

{
    "page": 2,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        {
            "id": 7,
            "email": "[email protected]",
            "first_name": "Michael",
            "last_name": "Lawson",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg"
        }
// the output trimmed because of space constraint
    ]
}

Environment Variable Vs Global Variable

  • Environment Variable is used when a change in Environment requires a change in the entire URL. On the other hand, Global variable is used when all the requests in all the environments have a common part in their URLs.
  • An Environment variable can also help in changing the credentials, for example in the case of retrieving data when a particular attribute is matched. On the other hand, the Global variable cannot be used to change the credentials, they will act the same irrespective of change of environments.
  • The variable set in one Environment will not interfere with the other variables in the Environment. On the other hand, the change in the Global Variable, the change in the variable value will affect the entire Request URLs.
  • Environment variables can have the same value in different environments. Other the other hand, Global variables cannot have multiple values for one variable name.
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions