Post request - to upload a lot of files but only one at a time

Hi Community! I have the next challenge: I need to create a collection where I need to upload a lot of files. Important here that it should be one file at a time (not bulk upload). My idea was to use a variable in “form-data” with file type, but it is impossible to set a variable there. Is that a supported feature? Or it is not?

I use mac Intel. And Postman (free) version is:
Version 11.18.1
UI Version: 11.18.1-ui-241029-1240
Desktop Platform Version: 11.18.0 (11.18.0)

Have a look at the following topic on Stack Overflow.

Can Postman take a file as a variable from a path? - Stack Overflow

I can see two potentials solutions here.

This one.

https://stackoverflow.com/a/64954126/22769054

But I actually prefer this one as it doesn’t require you to hack the collection JSON.

https://stackoverflow.com/a/74832159/22769054

You are basically overwriting the body in the pre-request script.

You would read the file name from your CSV or JSON data file. Then retrieve the file name for the current iteration and then overwrite the body.

Something like… (untested)

let src = pm.variables.get("fileName"); // Get file name from data file - needs to match the relevant column header or JSON key

pm.request.body.mode = "formdata";

pm.request.body.formdata = {
    "key": "file", // or change to your prefered key name
    "type": "file",
    "src": src
}; 
1 Like

Thanks Mike! You are fantastic!
I also tried the first solution and it worked but I had doubts as it is a hack.
The second option looks more correct.
Thank you for your additional comments as well! they make a lot of sense

1 Like