What are the "local" variables in Postman?

Viewed 15514

Postman's documentation leaves a lot to be desired. In their Variables page they say:

The following scopes are available to you:

  1. Global
  2. Environment
  3. Local
  4. Data

There's information about the Global and Environment scopes, and I believe the "Data" scope is the data from a collection run. But what are the "local" variables?

Because I'd love to have a variable that is calculated on the fly, used for the request, and then discarded. Both global and environment variables are persistent.

2 Answers

According to the Postman Quick Reference Guide local variables are only available within the request (or collection run) that has set them. So they are used for the request or collection run and then discarded.

When to use:

passing data from the pre-request script to the request or tests or between requests.

The behavior is a bit different in Postman vs Collection Runner / Newman, so make sure you understand how they work before using!

Setting

pm.variables.set('myVariable', MY_VALUE);

Getting

pm.variables.get('myVariable', MY_VALUE);

Removing

Local variables are automatically removed once the tests have been executed / collection run finished.

Local variables are the one you use in your Tests part. You may even use the 'let' declaration as it is coded in javascript ... ie:

let jsonData;
jsonData = JSON.parse(responseBody);

or use var for declaration.

var jsonData = JSON.parse(responseBody);

Though, you can erase globals on the fly using

pm.environment/global.unset(<variable>)

see here for details

Related