17

I am trying to upload a file with POSTMAN to this URL

http://localhost:3000/bucket/test/files/

And should got result in my controller there :

    put(request, response, args) {
    //HERE IN THE REQUEST.BODY 
    console.log(request.body)

    let fileManager = request.modules.VMFile;
    let mimeTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/x-icon', '  video/mpeg', 'text/html', 'video/x-msvideo', 'application/msword', 'application/pdf', 'application/vnd.ms-powerpoint', 'application/x-rar-compressed'];
    let maxFileSize = 4 * 1024 * 1024;

    fileManager.initUpload(mimeTypes, maxFileSize);

    fileManager.receive((files) => {

        fileManager.forEachFileContent(files, (file, content) => {

            minioClient.putObject(request.body.bucket, request.body.name, content, file.size, file.mimetype, function (err, etag) {
                response.setData("File uploaded").apply();
                return console.log(err, etag)
            })

        });
        fileManager.clearFilesFromTmp(files);
    });
}

In POSTMAN I got this :

postman

With nothing on headers but I could only PUT (or POST, I tried to change my route with POST but same issue) the name and bucket field... I got nothing on my files field...

Cupkek05
  • 458
  • 1
  • 6
  • 22
  • if this is express application, you need to write `app.put(, handler_function)`. your code snippet is incomplete. – zmii Sep 18 '17 at 15:03
  • It is a private API based on express, but my snippet is working – Cupkek05 Sep 18 '17 at 15:05
  • @Cupkek05 Just to clarify, you're getting nothing in your controller when postman fires the request over the network? Also, do you not need content-length and content-type on your headers? – Daniel Lane Sep 18 '17 at 15:06
  • @DanielLane I dunno because I checked some topic where they advocate to let the content-type empty, Ill try it – Cupkek05 Sep 18 '17 at 15:10
  • @Cupkek05 It looks like David's suggestion might work for you, give that a try. – Daniel Lane Sep 18 '17 at 15:11

4 Answers4

43

While using Postman especially when you test file upload ensure that,

  1. in Headers:
    • The Content-type field has been set as multipart/form-data in Headers.
  2. in Body:
    • form-data option should remain as default.
    • Choose File option instead of text from the dropdown on the right side.
    • Type File in the text box where the placeholder is key.
David R
  • 14,711
  • 7
  • 54
  • 72
  • 1
    Add content-type with multipart-formdata worked, thanks for your help – Cupkek05 Sep 18 '17 at 15:29
  • 1
    how to send data with content-type: application/json – rammanoj Dec 14 '18 at 05:22
  • @rammanoj Under "Headers" tab (in POSTMAN) you need to add key "Content-Key" (if you start typing you will get auto suggestion) and in the next box you can give "application/json" (again, when you start typing you will get auto-suggestion. Hope this helps! – David R Dec 14 '18 at 07:04
  • Thank you! Solved my problem. I've lost hours just because I was not doing the last bit `Type File in text box where placeholder is key.` ‍♂️ – Marlon Barcarol Mar 20 '20 at 12:22
  • Super helpful !! thanks a ton !! – Tom Taylor Oct 01 '21 at 11:37
  • That's really helpful, wasn't aware we can send file using put also, added step by step guide here: https://qawithexperts.com/article/web-api/upload-or-send-file-in-postman/438 – Jyoti Jul 25 '22 at 07:13
2

You might be doing it right but sometimes POSTMAN does not work well. I wrote an API to accept both text and file.
While invoking service from Postman.

  1. I set Content-Type as "application/json" and Accept as "application/json".
  2. In body I pass the text and file It was not working, I tried multiple times. I shut down postman and my laptop.

Woke up the next morning hit again and it worked. Below is the image of the working request.

enter image description here

1

Not all of David's answer worked for me when using express-fileupload. In short, I don't require the Content-Type header when using express-fileupload.

Note, these steps are for npm / express-fileupload / Postman

  1. Ensure the header Content-Type is disabled. Setting this forces req.files to be undefined.
  2. As per Davids answer, use form-data and set the key to whatever you require, and upload the file.
  3. If you console.log(req.files.YOUR_KEY) in your express app, you should have an object with the uploaded file, in my case req.files.file.

Here's what postman should look like (once again, disable the Content-Type header):

Postman confirugration

Here's the output within the console when using console.log(req.files):

Debug console

steadweb
  • 15,364
  • 3
  • 33
  • 47
0

The accepted answer was not clear enough, for googlers try the below extra steps:

  1. Headers:
Content-type: multipart/form-data
  1. Body:
    • Add key (file or name of your choice) and change type field for file.
    • Add key (_method) with value: PUT

The route sent must be POST.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Mubarak
  • 1
  • 1