3

Postman supports setting environment variables and using them in, say, headers.

Consider the following example:

  1. Token endpoint /auth/token requires basic Authorization header: Basic {{AUTH_BASIC}}
  2. All other endpoints requires bearer Authorization header: Bearer {{AUTH_TOKEN}}

Currently, I need to go through all the following steps, which is very annoying:

  1. Call /auth/token
  2. Manually copy the token from the response
  3. Go to Settings > Manage Environments > MyEnvironment
  4. Paste the token against AUTH_TOKEN and click Update

Is there a way to automatically update the AUTH_TOKEN environment variable each time when I call /auth/token or at least always display list of current environment variables so I can manually update values without going to Settings > Manage Environments > MyEnvironment?

Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86

2 Answers2

5

Considering the /auth/token response is something like:

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
}

Go to the Tests tab of /auth/token endpoint and paste the following:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("AUTH_TOKEN", jsonData.token);
Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86
1

For anyone landing here in the future, you can now accomplish this with the following:

var response = pm.response.json();
pm.environment.set('AUTH_TOKEN', response.token);
Jay Figaro
  • 303
  • 3
  • 10