# Loop a post request

**URL:** <https://community.postman.com/t/loop-a-post-request/10040>\
**Category:** Help Hub\
**Created:** [January 13, 2020, 8:46am UTC](https://community.postman.com/t/loop-a-post-request/10040 "2020-01-13T08:46:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![theToncheff](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@theToncheff](https://community.postman.com/u/theToncheff)\
**Post date:** [January 13, 2020, 8:46am UTC](https://community.postman.com/t/loop-a-post-request/10040/1 "2020-01-13T08:46:01Z")

</div>

Hello, I’m trying to figure out the logic for looping a request:  
I have a post request, that would use data from an array created in a previous request:  
array = [data1, data2, data3, data4]

So I have a ‘For’ cycle, similar to :

```
    for (i = 0; i < array.length; i ++){
    let userData = array[i] }

```

I’m using data-raw json for the request, so the idea is to loop it so that it runs the request once for each index of the array, so first run would be userData - data1, and then again with data2 and so on.

I’ve tried to add - I’m working on it as am creating this post, but need help

```
if (array.length === i) {
    console.log('done');
    return;
}
postman.setNextRequest('myRequest');

```

**This is my current attempt:**

```
var index1 = parseInt(postman.getEnvironmentVariable('index1'));
var profileArrayID = postman.getEnvironmentVariable('profileArrayID');
var organizationArrayID = postman.getEnvironmentVariable('organizationArrayID');

index1 = index1 + 1;
let userId = profileArrayID[index1];
let organisationId = organizationArrayID[index1];

if (profileArrayID.length === index1){
    console.log("Array done");
    return;
    
}
pm.environment.set("userId", userId);
pm.environment.set("organisationId", organisationId);
postman.setNextRequest('randomizedSignUps');

```

However it just runs the array by default value of index, aka array[index] with index being 0, how do I get it to get the index to be index = index + 1 for each request run, so first time 0, then 1, then 2 until the array.length is equal to the index

**Last edit** It seems I got it working, had to update environment for index1, so currently I have index1 = -1 in the environment, also having profileArray.length === index1 - 1, as it lead to having 1 duplicate post without the ‘-1’ , anyway it all seems to work well - as intended with the exception that there is 1 more request which fails as it has ran through the array and seems to try to use undefined value.

So in the end, once the array.length = index (without -1), the nextRequest stops running, however it runs one last time, making a duplicate entry

---

<div class="post-metadata">

**Author:** ![Sandokhan](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@Sandokhan](https://community.postman.com/u/Sandokhan)\
**Post date:** [January 13, 2020, 2:02pm UTC](https://community.postman.com/t/loop-a-post-request/10040/2 "2020-01-13T14:02:23Z")

</div>

@theToncheff in this other post you can find one way to solve your problem, I guess!

> [@How to store a JSON in a variable and use it in the request body?](https://community.postman.com/t/how-to-store-a-json-in-a-variable-and-use-it-in-the-request-body/1881/11):
>
> In the first request, on test I’ll put this code: let Cities = JSON.parse(responseBody), CityIds = \_.map(body, (city) =\> city.id); // Store all the Cities Ids in the environment variable pm.environment.set('CityIdsList', JSON.stringify(CityIds)); // Store the next index of the array for City ids pm.environment.set('nextIndex', 0); // Store the active City ID pm.environment.set('activeCityId', CityIds[0]); Then, on the second request I’ll use this one: let CityIds = JSON.parse(pm.environme…

Look out and see if fits you!

🕶

---

<div class="post-metadata">

**Author:** ![theToncheff](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@theToncheff](https://community.postman.com/u/theToncheff)\
**Post date:** [January 14, 2020, 7:53am UTC](https://community.postman.com/t/loop-a-post-request/10040/3 "2020-01-14T07:53:21Z")

</div>

I don’t seem to be able to make it work with that either.  
Basically it works halfway either my way or the way you offered, once it runs the last index of the array, it returns and stops the nextRequest, however it tries to run one last time, giving me a 500 error as there is nothing to get from the array. I’m trying to get it not to run that last request, but fail to do so.

**edit** Could it be because everything is in the prerequest script?

---

<div class="post-metadata">

**Author:** ![Sandokhan](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@Sandokhan](https://community.postman.com/u/Sandokhan)\
**Post date:** [January 14, 2020, 2:46pm UTC](https://community.postman.com/t/loop-a-post-request/10040/4 "2020-01-14T14:46:58Z")

</div>

It could be, why dont you try to do all of this on Tests?

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [January 14, 2020, 7:46pm UTC](https://community.postman.com/t/loop-a-post-request/10040/5 "2020-01-14T19:46:11Z")

</div>

@theToncheff

Here’s an example Collection I threw together with a looped POST request:

Redacted: wrong link. Will repost soon.

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [January 16, 2020, 6:41am UTC](https://community.postman.com/t/loop-a-post-request/10040/6 "2020-01-16T06:41:55Z")

</div>

Sorry @theToncheff , wrong link. Here’s the correct one:

> **[Single request loop](https://documenter.getpostman.com/view/5293839/SWLk35Jp?version=latest)**
>
> This Collection gets a list of IDs, then loops a POST request until all IDs have been sent.
> 
> The layout is as follows:
> 
> 
> \*\*GET ID list:\*\* https://postman-echo.com/get
> 
> Description:
> \- Response contains an array of numbers to use as dummy data.
> 
> Test...
