How to iterate through response pages when the total number of pages is unknown

I am pretty new to Postmen, and have no idea how to solve this.
In the response, each category (itemOne, itemTwo, etc.) can have an unlimited number of items, and there is pagination (20 items per page), in format {{url}}?page=2.
My task is to go through the list and test if it contains IDs previously saved as environment variables. However, there is nothing in the response that would indicate which page I am on or how many pages there are in total. There is also no information in the headers. I have found some solutions, but none that are this “bare.”

{
“itemOne”: [
{
“id”: 1078,
“price”: null,

    },
    {
        "id": 1036,
        "price": null,
       
    },
    {
        "id": 1120,
        "price": 4805,
       
    },
"itemTwo": [
    {
        "id": 1289,
        "price": null,
        
    }
],
"itemThree": [
    {
        "id": 1293,
        "price": null,
       
    }
]

}

Is there really nothing else in the response when you hit that last page, or at least a count of the number of items in each array.

Are you in control of this API, are you able to discuss this with the developer and get them to add something? Either a count of the number of elements in the array, or a nextPage element, which should tell you if there is any more pages or not. Ideally it should have a previous and next page element. (Pretty sure that is best practice for pagination).

What happens if you try and hit a page that doesn’t exist? Does it error gracefully or just go page not found? Perhaps that can be used instead to break the loop with an IF statement that says IF status code = 200, then run your normal tests, ELSE if status code = 404 (or whatever code is returned if you try and get a page that doesn’t exist) then run these other tests and break the loop. (setNextRequest=null)

Unfortunately there is nothing like a counter, or any property that I could use. All properties are just describing the product. There might be a possibility to discuss this with the dev team, but that is the last resort.

When you hit the last page, there is still 202 Accepted, and an empty array.

If there is an empty array, then you can check for that and use that to end the loop.

If its an empty array, instead of undefined then this usually means that the API is responding ok so sounds like you would be safe to use this.

If you do have access to the DEV though, I would ask them first though. It wouldn’t be my last resort. You can also talk to the DEV and explain the purpose of the test and why its needed. The checking of the blank array being the fall back (or a temporary workaround) Following the best practice of KISS (keep it simple). Put the code\logic into the API where it belongs.

1 Like