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

**URL:** <https://community.postman.com/t/how-to-loop-over-a-get-request-in-postman-api-in-loop-with-different-dates/35821>\
**Category:** Help Hub\
**Tags:** loop, get-request\
**Created:** [June 1, 2022, 7:08am UTC](https://community.postman.com/t/how-to-loop-over-a-get-request-in-postman-api-in-loop-with-different-dates/35821 "2022-06-01T07:08:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![supply-administrato5](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@supply-administrato5](https://community.postman.com/u/supply-administrato5)\
**Post date:** [June 1, 2022, 7:08am UTC](https://community.postman.com/t/how-to-loop-over-a-get-request-in-postman-api-in-loop-with-different-dates/35821/1 "2022-06-01T07:08:21Z")

</div>

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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![hugobnt88](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hugobnt88](https://community.postman.com/u/hugobnt88)\
**Post date:** [June 2, 2022, 2:39am UTC](https://community.postman.com/t/how-to-loop-over-a-get-request-in-postman-api-in-loop-with-different-dates/35821/2 "2022-06-02T02:39:48Z")

</div>

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

Add these code In Pre-request Script:

```auto
// 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:

```auto
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);
}

```
