Jeevan Mahesha
Jeevan Mahesha

Reputation: 83

postman pm set environment variable

I will get the JWT token as response i need to set that JWT token as environment variable in postman this is my code

pm.test("access_token is working", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.access_token).to.exist;
});

pm.environment.set("jwt_token", pm.test);

and when ever the JWT token changes the postman environment variable should set as that new value

Upvotes: 6

Views: 15600

Answers (5)

Saeful Anwar
Saeful Anwar

Reputation: 11

I use code like this in Postman Tests script, and use "pm.globals.set" with variable name "jwt_token", and if "jwt_token" doesn't exist it will autogenerate

var jsonData = pm.response.json();
var currentToken = jsonData["access_token"];
pm.globals.set("jwt_token", currentToken);

try this, its works in my postman

Upvotes: 1

Tiger1234
Tiger1234

Reputation: 1

I was trying to create the variable manually and it will not work.

I had struggled from with this for some time, and could not figure out what it was not working, so these instructions from hoandy worked for me. basically it means let Postman create the variable for you and it will work.

Upvotes: 0

Alexander Borisov
Alexander Borisov

Reputation: 1199

Old interface:

Follow these steps.

  1. Click an eye icon in the top right-hand side corner of the Postman.
  2. Click add button in order to create a new environment in the Postman. Give it a name.
  3. Now when you have an environment, make sure that you selected it in the dropdown menu next to the eye icon(Initially, it says No Environment)
  4. Proceed to your POST method Tests menu, and write this code there pm.environment.set("TOKEN", pm.response.json().access_token) in order to fetch the token and place it into TOKEN variable inside of your Postman environment.
  5. Now, whenever you want to fetch your token, go to your GET method Authorization menu, choose the type of token you have(for example Bearer Token), and write your variable name of the token in the Token field. In our example {{TOKEN}}

New Interface. Update from 2021, December 4th:

  1. Go to your workspace from Workspaces in the main navbar.
  2. In the right-hand side corner of the Postman, make sure the No Environment is set in the dropdown menu.
  3. Click an eye icon next to the dropdown menu.
  4. Click add button inside of the No Active Environment field in order to create a new environment in Postman. Give it a name.
  5. Now when you have an environment, make sure that you selected it in the dropdown menu next to the eye icon(Initially, it says No Environment)
  6. Proceed to your POST method Tests menu, and write this code there pm.environment.set("TOKEN", pm.response.json().access_token) in order to fetch the token and place it into TOKEN variable inside of your Postman environment.
  7. Now, whenever you want to fetch your token, go to your GET method Auth menu, choose the type of token you have(for example Bearer Token), and write your variable name of the token in the Token field. In our example {{TOKEN}}

Upvotes: 7

hoangdv
hoangdv

Reputation: 16127

In Postman's Test scripts let append these code:

var jsonData = JSON.parse(responseBody);
pm.environment.set("jwt_token", jsonData.access_token);

The jwt_token variable will be update (create) in your Environments. This block code does not reference to your test scripts.

Postman doc.

Upvotes: 4

Danny Dainton
Danny Dainton

Reputation: 25851

This would be all you need to set the token:

pm.environment.set("jwt_token", pm.response.json().access_token)

Ensure that you have an environment created and selected in the drop down, in the top right of the app before making the request.

Upvotes: 10

Related Questions