Can Postman take a file as a variable from a path?
Asked Answered
S

5

12

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?

Steppe answered 27/7, 2020 at 18:20 Comment(0)
S
1

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.

Steppe answered 3/9, 2020 at 8:2 Comment(0)
P
16

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

Paez answered 22/11, 2020 at 12:31 Comment(0)
H
3

It's not possible to read local files with Postman (There are at least two issues concerning that in their 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.

Hurtful answered 2/9, 2020 at 14:33 Comment(1)
Thanks, I think my use case is slightly different though. I can get postman to read the data file using a runner (blog.postman.com/…) but it's getting it to attach a file where it's falling downSteppe
S
1

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.

Steppe answered 3/9, 2020 at 8:2 Comment(0)
B
1

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.

Beanfeast answered 29/11, 2021 at 15:23 Comment(0)
T
0

The solution is quite simple,

  • Make sure you have the latest version of postman
  • Go to postman settings to find your working directory and add the desired file to your postman working directory
  • In the body tab, select formdata
  • In the pre-request script tab, enter the code below.
pm.request.body.mode = "formdata";
pm.request.body.formdata = {
    "key": "preveredKey",
    "type": "file",
    "src": "fileName.extension"
}; 
Thermidor answered 17/12, 2022 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.