0
votes

I am working on a POSTMAN collection. Say, I have two separate postman environments with each having URL variables, lets domain1 & domain2. In my initial script in pre-request tab I want to get a list of all the environments available so I can switch them when I need to. How do I get the list of environments? Thanks,

2

2 Answers

1
votes

Thanks Christian Bauman. I was able to accomplish by doing following In postman Pre-request Script tab. The response will contain environment array with object having id, name, owner, uid properties. you can then call by id to get further details of an environment.

let options = {
  method: 'GET',
  url: 'https://api.getpostman.com/environments',
  header: {
    'x-api-key': 'PMAK-your own key goes here'
  },
  json: true
};

let envs = [];
pm.sendRequest(options, function(err, response) {
  if (!err) {
    let data = response.json();
    _.forEach(data.environments, function(item) {
      envs.push(item);
    });
    console.log(envs);
  } else {
    console.log(err);
  }
});
0
votes

It is not possible to select environment from scripts. the closest one can get, is to receive the name of the currently active environment: pm.environment.name