Upload a file with PUT/POST method on POSTMAN
Asked Answered
H

4

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

Huebner answered 18/9, 2017 at 15:1 Comment(5)
if this is express application, you need to write app.put(<URL_HERE>, handler_function). your code snippet is incomplete.Prober
It is a private API based on express, but my snippet is workingHuebner
@Huebner 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?Cavit
@DanielLane I dunno because I checked some topic where they advocate to let the content-type empty, Ill try itHuebner
@Huebner It looks like David's suggestion might work for you, give that a try.Cavit
A
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.
Aileenailene answered 18/9, 2017 at 15:5 Comment(6)
Add content-type with multipart-formdata worked, thanks for your helpHuebner
how to send data with content-type: application/jsonNegrophobe
@Negrophobe 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!Aileenailene
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. 🤦‍♂️Elevation
Super helpful !! thanks a ton !!Lordosis
That's really helpful, wasn't aware we can send file using put also, added step by step guide here: qawithexperts.com/article/web-api/…Picky
F
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

Fere answered 20/4, 2018 at 23:39 Comment(1)
Possible to get user.png size while uploading? Any idea on that scenario?Indeterminate
O
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

Outlay answered 19/9, 2021 at 21:10 Comment(0)
C
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.

Chian answered 23/4, 2023 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.