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