本文介绍了获取响应值并在变量邮递员中设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在拨打邮递员呼叫,并且它会以不同的标头进行响应,例如访问令牌客户端 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.

推荐答案



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"));





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

这篇关于获取响应值并在变量邮递员中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 06:59