How To Loop Between The "Pages"

I am totally a newbie on Postman and on API in general, wondering if someone can show me how to retrieve all pages into one or multiple JSON files in just one “send”, using the GET request details attached. The request limit is for 10000 data items per page.

Reading the thread below I understood that I need to write some code in the pre-request script and in the Tests but, honestly, I really don’t know where to start. Would be great if the code is able to fetch automatically the total number of pages available directly from the API request.

https://community.postman.com/t/how-to-get-responses-of-multiple-pages-from-an-api-that-has-offset-instead-of-page-numbers/23760/4

Is anyone able to help / explain how to do it in my specific case?

Details (like screenshots):

Hi Alex,

So from your example that you posted, there is way to get all the pages using a Collection Runner. If you are not familiar on how to use the Collection Runner I would suggest looking here https://learning.postman.com/docs/running-collections/intro-to-collection-runs/ to get a feel on how it works.

In broad strokes, here is how I would accomplish this:
First you would need to GET the first URL
in your Tests, write a script that would pull the number of pages

response = pm.response.json()
num_pages = response.meta.totalPages

then store that value where you can use it in a loop, I would use a collection variable as it allows you to manually set in case you want to run a single request from the collection:

pm.collectionVariables.set("num_pages", num_pages)

If you always want to start on the first page, I would recommend setting a variable there as well to do that like:

pm.collectionVariables.set("page", 1)

With that variable stored as a collection variable you can now move on to the second request
there you can use a variable in the request to determine what page you want:

from the image you can see that in the URL I have a {{page}} variable where the page number will go and below were the page key is stored is pointing to a page variable.

In your Test tab you would set-up how the next page would be determined. Here is a rough example that assumes your request name is “Specific Page”

let num_pages = pm.collectionVariables.get("num_pages")
let page = pm.collectionVariables.get("page")

page++
pm.collectionVariables.set("page", page)

if (page <= num_pages) {
    postman.setNextRequest("Specific Page")
} else {
    postman.setNextRequest(null)
}

And this will keep retrieving pages until it hits the “num_pages”

I hope this helps.

Hi,

many thanks for helping me! You have been so kind to answer.

So I added all the info you provided in the Tests tab but I got the error below:

As you can see I have an error. For sure I am not doing the right thing. I had a look also at the runner page but don’t know how should work here.

My general idea, but again I can be wrong, is that in the Pre-request script tab we should collect (theoretically) the total number of pages, starting always from page 1. Then in the Tests tab we should write a loop code (the one you wrote) to fetch all pages and to save (if possible) one JSON file only (otherwise then I have the problem: how to merge paged JSON files?).

This it is supposed to work with just one “SEND” (Get) command/click?

What I do not get if I have to run first the script with some specific instruction and then re-run it again changing the Tests tab so to fetch all the pages in just one shot (one SEND button pressed).

Are you able to better clarify where, in which tab, should I copy your codes and what GET syntax should I run pls ?

Many thanks for you valuable support.

Hello,

There needs to be 2 separate calls in order for this to work and must be run in a Collection Runner setup.

The first call will grab the “num_pages” variable and set it.

The second call will be responsible for looping through.

The first call would contain this:

response = pm.response.json()
num_pages = response.meta.totalPages
pm.collectionVariables.set("num_pages", num_pages)
pm.collectionVariables.set("page", 1)

Then have the 2nd call which will contain:

let num_pages = pm.collectionVariables.get("num_pages")
let page = pm.collectionVariables.get("page")

page++
pm.collectionVariables.set("page", page)

if (page <= num_pages) {
    postman.setNextRequest("Specific Page")
} else {
    postman.setNextRequest(null)
}

I hope this explains it a little bit better.

Hi @dburns74 ,

I found this thread and was able to implement everything you suggested successfully, apart from how to actually download the responses from each page into a json file. Do you have any suggestion there? Ideally I’d like to have them in a single file, but even multiple I think I could work with.

Thanks!
Kyle