Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Postman take a file as a variable from a path?

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:

enter image description here

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?

like image 313
Kristan Avatar asked Jul 27 '20 18:07

Kristan


People also ask

How do I create a dynamic variable in Postman?

' 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.

What is PATH variable in Postman?

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).


Video Answer


4 Answers

Issue:

In postman if we check the UI, we notice that there is no way to define file path as variable.

enter image description here

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

enter image description here

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)

enter image description here

like image 73
PDHide Avatar answered Oct 22 '22 15:10

PDHide


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.

like image 44
Christian Baumann Avatar answered Oct 22 '22 15:10

Christian Baumann


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.

like image 1
Kristan Avatar answered Oct 22 '22 15:10

Kristan


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.

like image 1
Igor RUTSCHKOWSCAYA Avatar answered Oct 22 '22 14:10

Igor RUTSCHKOWSCAYA