ElConrado
ElConrado

Reputation: 1640

Pre-Request Script scopes in Postman

I have collection named "Auth" with pre-request script where I define JavaScript variable "token", and some JavaScript general functions, which I want to use in my inner pre-request scripts.

Within "Auth" collection I have folder name "Tests" with own pre-request script. I expected both scripts to be merged but when I try to use "token" JavaScript variable from "Auth" collection I get an error:

ReferenceError: currentAccessToken is not defined

How to access JavaScript variables (and JavaScript functions) from collection pre-request script in inner folder pre-request script?

Upvotes: 2

Views: 1840

Answers (1)

Christian Baumann
Christian Baumann

Reputation: 3434

Postman has different variable scopes, which you would need to use, eg:

pm.globals.set("variable_key", "variable_value");
pm.globals.get("variable_key");

pm.collectionVariables.set("variable_key", "variable_value");
pm.collectionVariables.get("variable_key");

pm.environment.set("variable_key", "variable_value");
pm.environment.get("variable_key");

Please check out Using variables for more details.

Upvotes: 2

Related Questions