I have a postman collection, with a set of three API calls I'd like to chain together and feed with a data file using the runner function. Lets say they're:
/prepareUpload
/upload
/confirmUpload
and the output of each is needed for the next step. I'm happily pulling stuff out of the responses and putting them into variables ready for the next call, but the bit I seem to be falling down on is the /upload
needs a file
parameter of type file
, but Postman doesn't seem to let me set it to a variable:
I've tried exporting the collection, manually editing the json to force it to a variable and running that, so something like :
<snip>
{
"key": "file",
"contentType": "{{contentType}}",
"type": "file",
"src": ["{{fullpath}}"]
}
],
"options": {
"formdata": {}
}
where {{contentType}}
and {{fullpath}}
are coming from my data file, but it never seems to actually do the upload.
Does anyone know if this is possible?
' In the request URL section, a dynamic variable should be written in {{__}} format. Let's say you have to pass an integer number from 1 to 1000, so for that, you need to add {{$randomInt}}. Like the above example of the number variable, Postman supports so many other dynamic variables as well.
Path variable (i.e. /:id ) is a part of a request URL and can be used when you want to be able to fetch a different resource based on the parameter (e.g. id).
Issue:
In postman if we check the UI, we notice that there is no way to define file path as variable.
This looks like a limitation when we need to run file from different systems
Solution:
The solution is to hack the collection.json file. Open the json and edit the formdata src and replace it with a variable, let say file_path so : {{file_path}}
Now in Postman:
in pre request you can below code to set the path
pm.environment.set("file_path","C:/Users/guest/Desktop/1.png")
You can also save it as environment variable directly or pass through cmd using --env-var when using newman.
Note:
set access file from outside working directory as true (settings from top right corner)
It's not possible to read local files with Postman (There are at least two issues concerning that in there tracker on github: 798, 7210)
A workaround would be, to setup a server that provides the file, so you could get the data via a request to that server.
Ok, so found the answer to this, and the short version is - Postman can't do it, but Newman can :
https://github.com/postmanlabs/newman#file-uploads
It's a fair bit more effort to get it set up and working, but it does provide a solution for automating the whole process.
For Postman (as of Version 9.1.5), on Mac os, you can trick postman by naming a file in your shared directory with your variable name (ie. {{uploadMe}}). Then you choose this file (named as the variable) from the file selector and Voilà.
In my case the files I upload are located in the same shared directory and don't forget to set the shared directory in your postman settings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With