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.
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
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:
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”
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 ?
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.