How to get responses of multiple pages from an API that has offset instead of page numbers

I was (am) having a similar issue, with just a page # being the issue and having it dynamically set.

I managed to get it working in a collection by doing the following:

I set an environment variable for page

I used the following as a Pre-Request Script:

   const page = parseInt(pm.variables.get("page"));
if(!page){
    pm.variables.set("page", 1);
}

I then used the following in my Tests to set the page variable:

tests["Got users"] = responseCode.code == 200;
let page = parseInt(pm.variables.get("page"));
let jsonData = JSON.parse(responseBody);
if (jsonData.data.has_more_data) { 
    console.log("More items available");
    postman.setEnvironmentVariable("page", page+1);
    postman.setNextRequest('Get User List');
} else {
    console.log("No more items available");
}

This allowed me to retrieve all pages for my request. I know have to figure out how to save all the responses to one (or several) files, which I understand that Postman is not good at, but I am trying anyways.

I hope this helps.

1 Like