Reputation: 83
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
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
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
Reputation: 1199
Follow these steps.
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.{{TOKEN}}
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.{{TOKEN}}
Upvotes: 7
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.
Upvotes: 4
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