Postman 具有构建工作流功能,您可以在其中指定接下来要调用的请求。
到达 Req4 后,调用 Req2,它基于计数器在 Req1 之后。
这可以在邮递员请求窗口的Tests 选项卡中实现。
Pseudo code -
set 2 global/environment variables , iteration = <some number you need>, iteration_ref = 0
<In Req1 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
postman.setNextRequest('Req2')
<In Req2 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
postman.setNextRequest('Req3')
<In Req3 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
postman.setNextRequest('Req4')
<In Req4 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
{
postman.setGlobalVariable("iteration_ref",
Number(postman.getGlobalVariable("iteration_ref"))+1);
postman.setNextRequest('Req2')
}
或者仅在最后一个请求中,如果您对集合中设置的请求的顺序非常有信心。
<In Req4 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
{
postman.setGlobalVariable("iteration_ref",
Number(postman.getGlobalVariable("iteration_ref"))+1);
postman.setNextRequest('Req2')
}
确保您首先在集合中拥有 Req1(Post Request),在集合运行器中拥有 only 1 iteration。我们使用全局/环境变量进行迭代。
PS:我强烈建议使用一个简单的 python 或 js 脚本使用请求库来调用 API,上面的流程是一个机智的 hack。
参考 - https://learning.getpostman.com/docs/postman/collection_runs/building_workflows/