首页 > 解决方案 > 邮递员有没有办法从 GET 请求中读取响应数据,然后使用 IF THEN 语句来运行 POST 请求?

问题描述

我正在尝试运行邮递员发送 Get 请求的脚本,如果 get 响应包含某个变量,则运行 Post 请求。我该怎么做呢?

(还有一种方法可以每小时运行一次获取请求)

标签: apiserverpostman

解决方案


在先决条件中添加:

// set initial value
const method = pm.variables.get("method")
// set initial value as GET if method is undefined
method ? null : pm.variables.set("method", "GET")

// Set this as method
pm.request.method =method

在测试脚本中添加:

// the condition check
if (pm.response.json().somevalue === "somevalue") {
    
    //then change the method
    pm.variables.set("method", "POST")

    //call the same request again using setNExtRequest
    // pm.info.reqeustName gives current request's name
    postman.setNextRequest(pm.info.requestName)
}

推荐阅读