Using prerequest script to setting environmental variable from RestAPI result

HI,

I’m trying to share both a collections and environments among a team while protecting access to sensitive Access Keys. Company policy prevents saving these keys in a shared repository, such as Postman environment file.

I’ve used ENVKEY to set up a restful endpoint which responds back with the result of the Access Key. I’m trying to figure out how to create a prerequest script to:

  • make the endpoint call,
  • set an environment variable with the result

The api call is http://localhost:3000/envkey/AUTHSTRING_EMEA
and it returns back a JSON key value array. The script should parse the result and set a variable from the result. My current nonworking script is:

var endpoint = 'http://localhost:3000/envkey/AUTHSTRING_EMEA';
var xhr = new XMLHttpRequest();
xhr.open("GET", endpoint, false);
xhr.send();

console.log(xhr.status);
console.log(xhr.statusText);

pm.environment.set("authstring", xhr.result);

Check out pm.sendRequest …

https://www.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference