Get Response Value and Set in Variables Postman

2019-08-17 18:37发布

问题:

I'm making postman call and it responds with different headers e.g. access-token, client, uid etc I want to retrieve their values and save in environment variables so that I don't have to set access-token every time. Can anyone guide me how to do this. Thanks in advance.

回答1:

it responds with different headers

var access-token = postman.getResponseHeader("access-token");
var uid = postman.getResponseHeader("uid");

pm.environment.set("access-token", access-token);
pm.environment.set("uid ", uid);

OR shorter way

pm.environment.set("access-token", postman.getResponseHeader("access-token"));
pm.environment.set("uid ", postman.getResponseHeader("uid"));

if it responds access-token, client, uid in response body then below is the way to get and set them -

var jsonData = JSON.parse(responseBody);
var uid = jsonData.uid;
var access-token = jsonData.access-token;

pm.environment.set("access-token", access-token);
pm.environment.set("uid ", uid);

OR shorter way -

pm.environment.set("access-token", jsonData.access-token);
pm.environment.set("uid ", jsonData.uid);

note- this will only work if response body is only one json object, in other cases path will change to access the required values.

FYI - You might wondering why there is pm and postman



标签: postman