postman pm set environment variable
Asked Answered
S

5

6

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

Spectacled answered 23/4, 2020 at 15:7 Comment(3)
Can you describe the problem/issue/error you are facing?Heffernan
i need to set postman environment variable which i get from responseSpectacled
Did one of the responses answer your question? If so, please mark that as the answer/upvote issues. If not, let us know what is missing.Lowelllowenstein
N
9

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.

Nies answered 23/4, 2020 at 16:25 Comment(0)
C
7

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}}
Coranto answered 10/11, 2020 at 8:41 Comment(0)
K
4

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.

Kalidasa answered 23/4, 2020 at 16:36 Comment(0)
S
1

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

Superficial answered 23/1, 2023 at 20:16 Comment(0)
D
0

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.

Decker answered 10/1, 2023 at 13:37 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewStylograph

© 2022 - 2024 — McMap. All rights reserved.