使用postman获取分页响应中的所有页面

使用postman获取分页响应中的所有页面,postman,Postman,上面的示例是我在响应正文中收到的。我正在尝试自动化邮递员自动拉每一页。我在postman中看到了关于条件工作流的文档,但我似乎无法让它正常工作。我使用了来自的一个示例,但该示例似乎与我的情况不太相符。请参阅下面的测试代码 { "users": [...], "next_page": "https://junk.example.com/api/v2/users.json?page=2", "previous_page": null, "count": 1091 } 正如您可能已经看到的,您已经

上面的示例是我在响应正文中收到的。我正在尝试自动化邮递员自动拉每一页。我在postman中看到了关于条件工作流的文档,但我似乎无法让它正常工作。我使用了来自的一个示例,但该示例似乎与我的情况不太相符。请参阅下面的测试代码

{
"users": [...],
"next_page": "https://junk.example.com/api/v2/users.json?page=2",
"previous_page": null,
"count": 1091
} 

正如您可能已经看到的,您已经尝试对其进行更改,以查看提取下一个页面对象的不同方法是否能够解决问题,但到目前为止没有成功。我没有收到任何错误get请求在我尝试执行收集运行时不会运行下一页

问题是postman.setNextRequest()的工作原理略有不同:在集合运行程序执行期间,它根据集合中请求的名称设置下一个请求。在您的例子中,您提供的不是集合中下一个请求的名称,而是您想要遵循的实际URL

发送此类请求的最简单方法是在集合中添加新请求,该请求可以命名为
'NextRequest'
,并提供环境变量
{{nextRequestUrl}}
作为URL

在第一个请求中,可以执行以下代码将下一页的实际URL添加到环境变量中

try {
    var jsonData = pm.response.json();
  //var jsonData = JSON.parse(responseBody);

  //The above commented code is my attempt to alter the original example 
  //in the hopes of a solution.

  postman.setNextRequest(jsonData.next_page);

} catch (e) {
  console.log('Error parsing JSON', e);
  postman.setNextRequest(null);
}

在您的
'NextRequest'
请求中,您可以在测试选项卡上添加相同的脚本。

您可以在预请求脚本或测试选项卡上使用pm.sendRequest。在下面的例子中,我已经打开了测试选项卡。使用“显示邮递员控制台”查看结果:

const jsonData = pm.response.json();

if(jsonData.next_page != null){
    pm.environment.set('nextRequestUrl', jsonData.next_page)
    postman.setNextRequest('NextRequest');
} else {
    postman.setNextRequest(null);
}
# Recursive function that will call all of the pages
function callMyRequest(nextUrl) {
    pm.sendRequest({
        url: nextUrl,
        method: 'GET'
    }, function (err, res) {
        if (err) {
            console.log(err)
        } else {
            # Basic tests about the response (res)
            pm.test("Response code is 200", function() {
                pm.expect(res).to.have.property('code', 200);
            });
                
            # Check for the next page
            if (res.json().next_page !== null) {
                callMyRequest(res.json().next_page);
            } else {
                # Stop iterations
                postman.setNextRequest(null);
            }
        }
    });
}

# Basic tests about the main response (pm.response)
pm.test("Response code is 200", function() {
    pm.expect(pm.response).to.have.property('code', 200);
});

# We need to check if there is pagination   
if (pm.response.json().next_page !== null) {
    callMyRequest(pm.response.json().next_page);
} else {
    postman.setNextRequest(null);
}