通过json迭代,对其他请求进行多次API调用

问题描述 投票:0回答:2

我正在使用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"
    }
]
java arrays api postman
2个回答
0
投票

这可能有助于获取数据 - 我还没有尝试过,但它可能不会第一次工作。

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将成为名单中的第一个。

对您的问题不是一个完整的解决方案,但它可能有助于您获取数据。


0
投票

我能够使用这两个指南来解决这个问题:https://thisendout.com/2017/02/14/loops-dynamic-variables-postman/https://thisendout.com/2017/02/22/loops-dynamic-variables-postman-pt2/

我还必须为java实现bigint修复,但在Postman中,这非常烦人......可以在这里找到:https://mahasak.com/hacking-bigint-in-api-testing-with-postman-runner-newman-in-ci-environment-eb69961d16fchttps://gist.github.com/mahasak/8d6ee6f0a5ddd3a9866f8b4df09d4e28

很多谷歌加试验和错误让我开始运行。

无论如何,谢谢大家的帮助!

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

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");
}
© www.soinside.com 2019 - 2024. All rights reserved.