首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过RestAPI POST调用进行迭代

问通过RestAPI POST调用进行迭代
EN

Stack Overflow用户
提问于 2019-01-23 10:54:12
回答 1查看 177关注 0票数 0

我正在使用一个用于创建和测试虚拟机的私有云平台。它们有丰富的API,允许我创建VM:

代码语言:javascript
复制
{
  "name": "WIN2016-01",
  "description": "This is a new VM",
  "vcpus": 4,
  "memory": 2147483648,
  "templateUuid": "sdsdd66-368c-4663-82b5-dhsg7739smm",
...
}

我需要通过简单地迭代-01部件来自动化创建机器的过程,因此它变成了:

  • “姓名”:“WIN2016 2016-01”
  • “姓名”:“WIN2016 2016-02”,
  • 名称:“WIN2016 2016-03” 等。

我试着使用Postman并构建工作流workflows/,但是没有运气--不确定我需要在测试选项卡中使用什么语法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-24 09:30:16

这是一种方法。

创建一个集合和发布请求。

在pre-request中添加以下内容:

代码语言:javascript
复制
/* 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)

在您的帖子请求体中,可以这样做:

代码语言:javascript
复制
{
   "name": "WIN2016-{{countString}}",
   ...
}

现在,通过'Collection Runner‘运行您的集合,并输入迭代的次数(例如,您希望您的集合运行多少次)。您还可以添加一个延迟,如果您的API施加了速率限制。

最后,单击Run。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54325589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档