I have a collection variable defined (Collection > Edit > Variables tab. Variable name: token) that Iām using in as request header value. I want to set its value in script. Iām trying to set it with this:
but the Collection variable value doesnāt change. In the past Iāve used pm.globals and pm.environment, and those work here as expected, but this variable is actually scoped to the collection. Iād rather work with it within that scope than use the broader globals or environment scope.
Hopefully this will help you out a bit. If the variable is not static, and I assume thats the case if its a token. You will likely need to scope this as an environment variable, you can also unset it at the end of the tests if need be.
Yes, @tmccann, that helps. It appears then that Collection variables are static whereas Environment and Global variables are dynamic. Perhaps the documentation could be clarified to this effect? Another option is to make Collection variables dynamic (and consistent) with Environment and Globals.
Let me renew this topic, I do love Postman but it comes with lot of frustration about this variables management.
I maintain collections in my workspace, with my environment, I define and then update variables on pre-scripts and tests :
pm.environment.set("my_token_id", jsonData.uid);
But when it comes to sharing my collections, if the receiver donāt have any environement, then no variable is set. So I ended setting the environment AND the global variables :
But as collections are the way to easily export and share your API or a special workflow, it would be so much elegant to handle all variables into this collection.
The ability to programmatically get and set variables, at the collection level, has been part of the app for a while now. Hopefully, this new functionality will help solve some of the issues youāre facing.
To use them in the app (Pre-request Script and Tests sandboxes), itās just a case of using the collectionVariables method on the pm.* API. This new method works in the same way as you would use the get, set, unset and clear functions on the variables, at the Environment and Global level.
Weāre in the process of updating the documentation in the Learning Center so hopefully, this will become a lot clearer soon.
The Learning Center is open source and weāre always looking for willing folks to help improve the content for other users - If youāre up for contributing, that would be awesome.
At the top of each page, you will find this button, this will link to the repo!
I am trying to set an environment variable for a particular request but after the request is run, the value is not assigned to the variable even though the variable is created from that script, the values are empty. Please advise.
I figured out the problem while I was getting the information to send to you together. The response for this particular request is in an array and I didnāt cater for that in the script I wrote to set the variable. Thank you.
I am using version 7.17.0 on the mac. The pm.collectionVariables.set() simply does not work. Just try something as easy as
pm.collectionVariables.set(āmy_varā, 'some-value);
console.log("my_var = " +pm.collectionVariables.get(āmy_varā));
In the collections tab on the left panel , firstly click on the 3 dots and choose edit.
After doing so, already set your variable name as access_token.
Then in the test script, I would always suggest to place the following code: this helps to see the status every time:
pm.test('Status code is 200', function () {
pm.response.to.have.status(200);
});
After doing so paste this code:
var jsonBody = pm.response.json();
pm.collectionVariables.set("access_token",jsonBody.access_token);