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

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}};

  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:

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:

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 but I can’t make it work. :frowning:

This isn’t being assigned to a variable.

Shouldn’t it be

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

Hi! That’s my bad for committing such obvious mistake. :sweat_smile: Thank you for pointing it out. :wink:

So after fixing variable assignment it (almost) worked as intended but I got another error that pm.setNextRequest is not a function but THAT post helped me with that error. Working code is below:

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)");
}