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?
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}}.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With