Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign value to variable in Postman

I'm trying to write a postman collection test and I'm struck at a point where I need to assign a value to global variable and use it in another api call.

Here it goes: The api response is like this:

{
    "status": "success",
    "code": 200,
    "data": {
        "expires_time": 10800,
        "authentication_token": "access-token",
        "refresh_token": "refresh-token"
    }
}

The test I'm writing is something like this:

tests["Status code is 200"] = responseCode.code === 200;

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("Authorization", jsonData.data.authentication_token);

Thoughts?

like image 271
Vighnesh Pai Avatar asked Jun 15 '17 15:06

Vighnesh Pai


People also ask

How do I add dynamic values to my Postman?

A dynamic variable name starts with '$. ' In the request URL section, a dynamic variable should be written in {{__}} format. Let's say you have to pass an integer number from 1 to 1000, so for that, you need to add {{$randomInt}}.

How do you set a response as a variable in Postman?

All you have to do is call postman. setEnvironmentVariable(key, value) or postman. setGlobalVariable(key, value) to set a variable with values you have extracted from the response.

Which method is used to set the value of and environment variable in Postman?

To set up Postman environment variables: In the top right corner of Postman, click the environment selector and select Manage environments. Click Add to add a new environment where you'll define your OneLogin environment variables. Note: You'll need to use your API credentials to generate the access_token value.


1 Answers

You are saying that you need to assign global variable while your code is trying to assign environment variable, which is different.

Assigning global variable looks like following:

postman.setGlobalVariable("variable_key", "variable_value");

Make sure to create global variable with empty value in the Postman UI first, and than you will be able to assign value to it using above piece of code.

like image 110
sen4ik Avatar answered Sep 28 '22 06:09

sen4ik