获取响应值并在变量邮递员中设置 [英] Get Response Value and Set in Variables Postman
问题描述
我正在拨打邮递员
呼叫,并且它会以不同的标头
进行响应,例如访问令牌
,客户端
, uid
等检索它们的值并保存在环境变量
中,这样我就不必每次都设置 access-token
。谁能指导我该怎么做。
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.
推荐答案
它以不同的标题响应
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 较短的方式
pm.environment.set("access-token", postman.getResponseHeader("access-token"));
pm.environment.set("uid ", postman.getResponseHeader("uid"));
如果它在响应主体中响应访问令牌,客户端,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 较短的方法-
pm.environment.set("access-token", jsonData.access-token);
pm.environment.set("uid ", jsonData.uid);
注意-这仅在响应主体仅是一个json对象时起作用,在其他情况下路径将改变来访问所需的值。
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
这篇关于获取响应值并在变量邮递员中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!