【问题标题】:Iteration through json with multiple API calls for other requests对 json 进行迭代,并对其他请求进行多个 API 调用
【发布时间】:2018-06-04 16:23:29
【问题描述】:

我正在使用 Postman 遍历大约 40 对项目的 json。然后我需要创建该数组并为数组中的每个元素运行 API 调用以返回一组结果。使用此处的代码,我只能提取数组中的最后一个元素。我试图将 postman.setNextRequest 放在 for 循环中,但后来我发现无论它在哪里,它总是最后执行。

tests["Status code is 200 (that's good!)"] = (responseCode.code === 200);

if (responseCode.code === 200) {

var jsonData = pm.response.json();
var json = [];

postman.setEnvironmentVariable("json", jsonData)
postman.setNextRequest('GetAdmins');

for (var key in jsonData ) {
    if (jsonData.hasOwnProperty(key)) {
        postman.setEnvironmentVariable("organizationId", jsonData[key].id)
        postman.setEnvironmentVariable("orgname", jsonData[key].name)
        tests[jsonData[key].name + " " + jsonData[key].id] = !!jsonData[key].name;
        }
    }
}

else {
    postman.setNextRequest(null);
}

GetAdmins 是另一个在调用中使用 {{organizationId}} 的 GET。

我想我正在寻找的是;在 json 中的每个元素上运行另一个 API 调用的最佳方法是什么?

提前致谢!

编辑:添加 JSON 输出

[
    {
        "id": XXXXXX,
        "name": "Name1"
    },
    {
        "id": XXXXXX,
        "name": "Name2"
    },
    {
        "id": XXXXXX,
        "name": "Name3"
    }
]

【问题讨论】:

  • var json = [] 在这里做什么。看起来你并没有向它推任何东西。看起来每次运行时它都会覆盖以前的环境变量集。此外,您可以考虑在 Postman 中使用 Lodash 将这些值映射到键。
  • 您能否也将pm.response.json() 的输出添加到问题中。
  • @DannyDainton - var json 被用作在 API 调用之间传递的环境变量。我会去看看 Lodash。
  • 我可以看到 json Postman 变量已设置,但您在上面的行中有一个 var json = [] 与它无关。看起来它试图在一个数组中收集一些东西,但它什么也没做。

标签: java arrays api postman


【解决方案1】:

这可能有助于获取数据 - 不过我还没有尝试过,所以第一次可能无法正常工作。

var jsonData = pm.response.json()

data = _.map(jsonData, item => {
         organizationId: item.id
         orgName: item.name
     })
pm.environment.set('organizationData', JSON.stringify(data))

然后,您将所有组织数据保存在一个变量中,您可以使用这些数据在下一个“获取管理员”请求中迭代 Id。

您需要在下一个请求的Pre-request script 中有一些代码来访问每个 id 以在请求中迭代。您需要像这样解析变量:

var orgID = pm.environment.get(JSON.parse("organizationData"))

那么orgID[0].organizationId 将是列表中的第一个。

不是针对您的问题的完整解决方案,但它可能会帮助您获取数据。

【讨论】:

    【解决方案2】:

    我能够使用这两个指南解决这个问题:

    1. Loops and dynamic variables in Postman: part 1
    2. Loops and dynamic variables in Postman: part 2

    我还必须为 java 实现 bigint 修复,但是在 Postman 中,这很烦人……可以在这里找到:

    1. Hacking bigint in API testing with Postman Runner Newman in CI Environment
    2. Gist

    大量的谷歌加上反复试验让我启动并运行。

    感谢大家的帮助!

    这最终成为我的最终代码:

    GetOrgs

    tests["Status code is 200 (that's good!)"] = (responseCode.code === 200);
    
    eval(postman.getGlobalVariable("bigint_fix"));
    
    var jsonData = JSON.parse(responseBody);
    var id_list = [];
    
    jsonData.forEach(function(list) {
        var testTitle = "Org: " + list.name + " has id: " + JSON.stringify(list.id);
        id_list.push(list.id);
        tests[testTitle] = !!list.id;
    });
    
    postman.setEnvironmentVariable("organizationId",JSON.stringify(id_list.shift()));
    postman.setEnvironmentVariable("id_list", JSON.stringify(id_list));
    postman.setNextRequest("GetAdmins");
    

    GetAdmins

    eval(postman.getGlobalVariable("bigint_fix"));
    
    var jsonData = JSON.parse(responseBody);
    
    jsonData.forEach(function(admin) {
        var testTitle = "Admin: " + admin.name + " has " + admin.orgAccess;
        tests[testTitle] = !!admin.name;
    });
    
    var id_list = JSON.parse(environment.id_list);
    if (id_list.length > 0) {
        postman.setEnvironmentVariable("organizationId", JSON.stringify(id_list.shift());
        postman.setEnvironmentVariable("id_list", JSON.stringify(id_list));
        postman.setNextRequest("GetAdmins");
    }
    
    else {
        postman.clearEnvrionmentVariable("organizationId");
        postman.clearEnvironmentVariable("id_list");
    }
    

    【讨论】:

    • bigInt 修复在您的上下文中做了什么?所有的解决方案都使用较旧的邮递员语法,所以我建议检查 pm.* api,因为这种较旧的语法最终将被删除。
    • @DannyDainton - 抱歉,呃,回复晚了。使用 Bigint 是因为返回的值非常非常大。没有它,Java/Postman 无法解释它看到的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    相关资源
    最近更新 更多