# Iteration of a specific request in a collection to upload multiple form-data

**URL:** <https://community.postman.com/t/iteration-of-a-specific-request-in-a-collection-to-upload-multiple-form-data/42553>\
**Category:** Help Hub\
**Tags:** variables, form-data, loop, incrementation\
**Created:** [January 3, 2023, 1:22pm UTC](https://community.postman.com/t/iteration-of-a-specific-request-in-a-collection-to-upload-multiple-form-data/42553 "2023-01-03T13:22:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![meatinaplasticsack](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meatinaplasticsack/32/13383_2.png) [@meatinaplasticsack](https://community.postman.com/u/meatinaplasticsack)\
**Post date:** [January 3, 2023, 1:22pm UTC](https://community.postman.com/t/iteration-of-a-specific-request-in-a-collection-to-upload-multiple-form-data/42553/1 "2023-01-03T13:22:59Z")

</div>

Hello,

I have this test case: I need to test uploading bigger file that is split into smaller chunks. I have 3 request in the collection:

1. I request upload URL. It has to be run once.
2. I need to upload all file chunks as a separate POST requests. **It has to be iterated.**
3. I declare, that all chunks are uploaded. It has to be run once.

To upload a file chunk, I use _form-data_. To distinct chunks during upload I need to provide couple of parameters in URL, mostly {{chunk}} (chunk number) and {{chunks}} (total number of chunks).

My way of thinking is that I should:

1. Store form-data file path as variable: {{chunk\_path}};

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/b/db1b78c9c258824cf712ca887d308ae94d54f256.png)

1. For every iteration I need a different file so I set **{{chunk\_path}} = {{path}} + {{chunk}}** where **{{path}}** (it is path to a file that doesn’t change, but without chunk number: C:\test\part) and **{{chunk}}** (chunk number). In the end {{chunk\_path}} looks like that: “C:\test\part0”.

2. To create iteration I use _if_ statement where I compare 2 variables and when statement is true, I increment one of variables and use it in next iteration. **This is the step that causes some problems.**

**My Pre-Request Script:**

```auto
pm.environment.get("chunks");
pm.environment.get("chunk");

pm.environment.set("chunk_path", pm.environment.get("path") + pm.environment.get("chunk"));

console.log(`This iteration chunk number is: ${pm.environment.get('chunk')}`);
console.log(`This iteration chunk path: ${pm.environment.get('chunk_path')}`);

```

This seems to be OK - I don’t get any errors.

**My Tests:**

```auto
pm.environment.get("chunks"); //100
pm.environment.get("chunk"); //0

if (chunk <= chunks) {
    pm.environment.set("chunk", chunk++);
    pm.setNextRequest("02. Perform the upload of chunks");
    
} else {
    pm.setNextRequest("03. Finish the upload (no more chunks)");
}

```

Here I get the error: **“ReferenceError: chunk is not defined”**

> How to correctly do the incrementation? Is my way of thinking correct? Maybe there’s some better way to achieve my goal?

I’m not a JS specialist. I used search on forum and google, tried some references like [JavaScript Tutorial](https://www.w3schools.com/js/default.asp) but I can’t make it work. ☹

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 5, 2023, 1:15pm UTC](https://community.postman.com/t/iteration-of-a-specific-request-in-a-collection-to-upload-multiple-form-data/42553/2 "2023-01-05T13:15:12Z")

</div>

> [@meatinaplasticsack](#):
>
> ```auto
> pm.environment.get("chunks");
> pm.environment.get("chunk");
> 
> ```

This isn’t being assigned to a variable.

Shouldn’t it be

```auto
let chunks = pm.environment.get("chunks");
let chunk = pm.environment.get("chunk");

```

---

<div class="post-metadata">

**Author:** ![meatinaplasticsack](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meatinaplasticsack/32/13383_2.png) [@meatinaplasticsack](https://community.postman.com/u/meatinaplasticsack)\
**Post date:** [January 9, 2023, 9:40am UTC](https://community.postman.com/t/iteration-of-a-specific-request-in-a-collection-to-upload-multiple-form-data/42553/3 "2023-01-09T09:40:31Z")

</div>

Hi! That’s my bad for committing such obvious mistake. 😅 Thank you for pointing it out. 😉

So after fixing variable assignment it (almost) worked as intended but I got another error that **pm.setNextRequest is not a function** but [THAT](https://community.postman.com/t/typeerror-pm-setnextrequest-is-not-a-function/3918) post helped me with that error. Working code is below:

```auto
let chunks = pm.environment.get("chunks"); //515
let chunk = pm.environment.get("chunk"); //0

if (parseInt(pm.environment.get("chunk")) <= parseInt(pm.environment.get("chunks"))) {
    parseInt(pm.environment.set("chunk", 1+Number(chunk)));
    console.log(`Chunk number after incrementation is: ${pm.environment.get('chunk')}`);

    postman.setNextRequest("02. Perform the upload of chunks");
    
} else {
    postman.setNextRequest("03. Finish the upload (no more chunks)");
}

```
