通过RestAPI POST调用进行迭代
我正在使用一个用于创建和测试虚拟机的私有云平台。他们有丰富的API,允许我创建虚拟机:通过RestAPI POST调用进行迭代,rest,api,automation,postman,Rest,Api,Automation,Postman,我正在使用一个用于创建和测试虚拟机的私有云平台。他们有丰富的API,允许我创建虚拟机: { "name": "WIN2016-01", "description": "This is a new VM", "vcpus": 4, "memory": 2147483648, "templateUuid": "sdsdd66-368c-4663-82b5-dhsg7739smm", ... } 我需要通过简单地迭代-01部分来自动化创建机器的过程,因此它变成: “名称”:“W
{
"name": "WIN2016-01",
"description": "This is a new VM",
"vcpus": 4,
"memory": 2147483648,
"templateUuid": "sdsdd66-368c-4663-82b5-dhsg7739smm",
...
}
我需要通过简单地迭代-01部分来自动化创建机器的过程,因此它变成:
- “名称”:“WIN2016-01”
- “名称”:“WIN2016-02”
- “名称”:“WIN2016-03”
等等
我尝试使用Postman Runner构建工作流,但没有成功-不确定在测试选项卡中需要使用什么语法。这是一种方法 创建一个集合和您的POST请求 在您的
预请求中
,添加以下内容:
/* As this will be run through the Collection Runner, this extracts
the number of the current iteration. We're adding +1, as the iteration starts from 0.*/
let count = Number(pm.info.iteration) + 1;
//Convert the current iteration number, to a '00' number format (will be a string)
let countString = ((count) < 10) ? '0' + count.toString() :
count.toString();
//Set an environment variable, which can be used anywhere
pm.environment.set("countString", countString)
现在,通过“collection Runner”运行集合,并输入迭代次数(例如,希望集合运行多少次)。如果API施加了速率限制,还可以添加延迟
最后,单击运行您可以在“请求前脚本”选项卡中设置变量,然后在请求本身上使用这些变量使其成为动态的。因此,您可以在每次请求后获得一个递增的数字,然后将其转换为字符串(例如,2将变成“02”),并将其附加到您的请求正文中。您认为我可以提供一些关于此问题的文档吗?我知道如何使用预请求脚本,但不知道如何实现这个特定的解决方案。
{
"name": "WIN2016-{{countString}}",
...
}