How to loop over a GET request in postman API in loop with different dates?

I am new to Postman and API calls, and am starting to read more on pre-request scripts and can’t figure out to loop over a certain data and save the responses to local machine.

I have a get request say, GET https://my-url.com/data?from=2021-01-01&to=2021-01-10

I would like to extract data from 01-01-2021 to 30-May-2022 (today’s date) and save the response (the data is returned as response). I cannot request the server for the complete data at once as there is a lot of data.

So, I would like to write a GET request and run it in a loop over a period of 10 days starting 01-01-2021.

Start loop
GET https://my-url.com/data?from=2021-01-01&to=2021-01-10
GET https://my-url.com/data?from=2021-01-11&to=2021-01-20
GET https://my-url.com/data?from=2021-01-21&to=2021-01-31
.
.
GET https://my-url.com/data?from=2022-05-21&to=2022-05-30
END LOOP

How to generate this date range and run the loop in postman, via collections or otherwise?

Hi @supply-administrato5 ,
Could help you in your scenarios

Add these code In Pre-request Script:

// DEFINE ARR VARIABLES
let FromDateArr =["2021-01-01", "2021-01-11", "2021-01-21", "2022-05-21"];
let ToDateArr = ["2021-01-10", "2021-01-20", "2021-01-31", "2022-05-30"];

let FromDates = pm.variables.get("FromDates");
let ToDates = pm.variables.get("ToDates");

if(!FromDates && !ToDates ) {
    FromDates = FromDateArr;
    ToDates = ToDateArr;
}

let currentFromDate = FromDates.shift();
pm.variables.set("FromDate", currentFromDate);
pm.variables.set("FromDates", FromDates);

let currentToDate = ToDates.shift();
pm.variables.set("ToDate", currentToDate);
pm.variables.set("ToDates", ToDates);

Tests Tab:

const FromDates = pm.variables.get("FromDates");
const ToDates = pm.variables.get("ToDates");

if (FromDates && FromDates.length > 0 || ToDates && ToDates.length > 0){
    postman.setNextRequest("Loop");
} else {
    postman.setNextRequest(null);
}