Local variable usage in subsequent script and request

Iā€™m running 4 requests as a part of 1 collection.

If I set a variable in Request1 using pm.variables.set("name", "Postman");
Iā€™m not able to access it in subsequent requests without pm.variables.get("name") within the same collection run
I understand this is because it is set as a local variable
I need to use pm.variables.get("name") in every request I want to use name.

But, I used the following to fetch UserId from my POST request:
const resData = pm.response.json();
const userId = resData.id;
//fetch the userID and set variable to be used in next request
pm.variables.set(ā€œuserIdā€, userId);

And Iā€™m able to use this userId in all of the subsequent API request calls without the need for get (These request are part of same collection and are run at once)

I do not understand how is the behaviour varying(userId vs name) in my script

Hi @krurai, Welcome to the Postman Community!

If it works for the ā€œuserIDā€ variable, it definitely should work for the ā€œnameā€ variable. Is it possible for you to share a screenshot of your script tab where this variables are being set?

Local variables are only available to that request or collection run.

Therefore, they wonā€™t be available to your second request unless you use the collection runner.

Store and reuse values using variables | Postman Learning Center

const resData = pm.response.json();
const userId = resData.id;
//fetch the userID and set variable to be used in next request
pm.variables.set(ā€œuserIdā€, userId);

In the above example, the const on the second line and the pm.variables.set are both local variables. With the same name, so please be wary of scoping issues.

You donā€™t actually need the pm.variables.set() in this scenario, as the initial ā€˜const userIdā€™ will be available to your second request as long as you use the collection runner.

Itā€™s also worth noting how you are using the variable in the second request. Local variables can only be used in the scripting tabs. (Pre-request and tests tabs).

They canā€™t be called in the body or URL like a collection, environment or global variable using {{variableName}};

While you can use local variables, its sometimes better to just use collection variables for storing these types of values.

Hi @gbadebo-bello,

This is my Request1


Iā€™m setting both userId and Appname

This is my Request2


The userId is being fetched from Request1 and there is no get function

But I see an error that Appname is not defined

POST https://gorest.co.in/public/v2/users
GET https://gorest.co.in/public/v2/users/6758642
ReferenceError: Appname is not defined

Hi @michaelderekjones,
Yes, Iā€™m using the ā€œRun collectionā€ option. I was trying to figure out how set, get works and came across this scenario.

I understand how ā€˜const userIdā€™ will be available in my second request, but Iā€™m curious why the other variable was not available and I needed a get function.

Could you please elaborate on using the same name

pm.variables.set(ā€œuserIdā€, userId);

Could you please elaborate on the potential scoping issues you mentioned with the variable name ā€˜userIdā€™? Iā€™m new to Postman and Iā€™m still trying to understand the best practices.

After some testing, it would appear that pm.variables.set is not the same as just setting it using const or let.

Variables set using pm.variables are available in places like the params, body and URL.

Even though when you hover over the variable, it tells you that its unresolved, but it does actually seem to work. (Logically it is unresolved, as the local variable is only set on run time).

For exampleā€¦

You can see from the console log, that the value set in the pre-request script is being read.

image

Variable set using const or let are not available unless you then use them to set one of the valid variable types.

Now weā€™ve got my initial misunderstanding out of the way.

The problem you are facing is that you canā€™t just console log the variable in the second request by its name.

You need to use the ā€œgetā€ command in scripts.

console.log(pm.variables.get("Appname"));

Using variables in scripts | Postman Learning Center

Using variables in scripts is different to how they are used in the URL, query parameters, body, etc.

Hello @krurai,

Iā€™m new to Postman too (just a couple of days).

  1. const and let variable are just local to the pre or test script, like in a module.

  2. to use a variable in the request definition (url, param, header etcā€¦) in the form {{userId}}, you must use one of the form :

  • pm.globals.set

  • pm.collectionVariables.set

  • pm.environment.set

  • pm.variables.set

    in a pre/test script (mostly in pre to use it in same request definition)

  1. as far as I know, the preceding order is used if you have the same name used for a global, a collection, an env, or a ā€œshadowā€ variable. Global is masked by collection, collection is masked by env, and the last wins it all.

  2. what I call (donā€™t know the exact terminology) ā€œshadowā€ variable is : the fact that using just pm.variables.set(ā€˜userIdā€™, ā€˜user001ā€™) will create a variable for the time of the run. It wonā€™t appear in any ā€œVariablesā€ tab.

  3. so far, best practice seems to be :

  • donā€™t use globals
  • use collectionVariables if you need to set it manuelly and it does not depends of an environment
  • use an environement variable when needed
  • prefer the last one if you only need it at run time and you set it in pre/test script
  1. all variable set by programmation in pre/test script keep there type (array, object/string, number) BUT as soon as youā€™re editing one in a ā€œVariablesā€ tab, it just turn to a simple string. Object/Array will then need to be json parsedā€¦

Hope this could help you too.

Best

Didier

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.