Currently, it is possible to set and get variables from the global
and environment
scope, as well as the generic variable
in a pre-request script. However, the documentation is not clear if it is possible to programmaticaly set collection
scoped variables.
For example
pm.environment.set("timestamp", timestamp); //acceptable pm.global.set("signature", hash); //acceptable pm.variable.set("signature", hash); //acceptable pm.collection.set("signature", hash); //not possible?
Is this possible?
You can use pre-request scripts in Postman to execute JavaScript before a request runs. By including code in the Pre-request Script tab for a request, collection, or folder, you can carry out pre-processing such as setting variable values, parameters, headers, and body data.
' 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}}. Like the above example of the number variable, Postman supports so many other dynamic variables as well.
You can only currently set these manually at the Collection level but you can reference these using the pm.variables.get('var_name')
syntax.
https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables#defining-collection-variables
EDIT:
Postman now allows you to use:
pm.collectionVariables.set('var_name', 'var_value')
and pm.collectionVariables.get('var_name')
to interact with the Variables at the Collection level.
https://stackoverflow.com/a/58325002/6028443
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