7

All the examples on learning.getpostman.com site show how to save variables at the global or environment level, in order to get a value from one request to another.

For example:

pm.environment.set(variableName:String, variableValue:String)
pm.environment.get(variableName:String)

pm.globals.set(variableName:String, variableValue:String) 
pm.globals.get(variableName:String)

respectively.

Is it possible to use Collections instead?

Reading the documentation about the pm. API doesn't give me much hope.

I am looking for something like this:

pm.collection.set(variableName:String, variableValue:String)
pm.collection.get(variableName:String)

My goal is to be able to run two collections without cross-contamination.

EDIT

Postman evolves. If you read the documentation it is also possible to set the variables in the collection, instead of in the environment. Be aware that if you have variables with the same name set in both places, using {{variablename}} may not result in the desired outcome. For example, if a collection variable and an environment variable have the exact same names, they are still two completely different variables.

JoSSte
  • 2,953
  • 6
  • 34
  • 54
  • Does this answer your question? [Accessing Collection Variables in Postman](https://stackoverflow.com/questions/47680580/accessing-collection-variables-in-postman) – Henke Jan 26 '21 at 16:49
  • @Henke I have edited my edit to clarify the sentence – JoSSte Jan 26 '21 at 18:30

2 Answers2

12

Postman version (7.9.0) and beyond, ships with a pm.collectionVariables function, this gives users more control over the way that they interact with Variables, at the Collection level.

Previously, you could only manually add these variables and then access them in the sandbox environment using the pm.variables.get("var_name") function.

The new pm.collectionVariables API method comes with the follows functions:

     .get("var_name")
     .set("var_name", "var_value")
     .has("var_name")
     .unset("var_name")
     .clear()
     .toObject()

An example of the syntax would be this:

pm.collectionVariables.set("collectionVariable", "My Awesome New Collection Level Variable");

This is a basic example of setting a Collection level Variable from the Tests tab:

Collection Variables

JoSSte
  • 2,953
  • 6
  • 34
  • 54
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
1

Update Your Variables.

And Then Update Your Pre-request script

const tokenUrl = 'http://localhost/api/login';
const email = 'admin@gmail.com';
const password = '12345678';

const getTokenRequest = {
  method: 'POST',
  url: tokenUrl,
    body: {
      mode: 'formdata',
      formdata: [
          { key: 'email', value: email },
          { key: 'password', value: password }
      ]
  }
};

pm.sendRequest(getTokenRequest, (err, response) => {
  const jsonResponse = response.json();
  const newAccessToken = jsonResponse.data.token;
  let token = pm.collectionVariables.get("token");
  pm.collectionVariables.set('token',newAccessToken);
});
JoSSte
  • 2,953
  • 6
  • 34
  • 54
Maizied Hasan Majumder
  • 1,197
  • 1
  • 12
  • 25