dmorrow
dmorrow

Reputation: 5694

Postman programmatically set collection variables in pre-request script

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?

Upvotes: 24

Views: 18141

Answers (1)

Danny Dainton
Danny Dainton

Reputation: 25921

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

Upvotes: 28

Related Questions