DEV Community

Daniel Benedek
Daniel Benedek

Posted on

Unexpected end of form when sending form data with Postman- Multer

I keep getting "Unexpected end of form" errors when sending a post request with form data using Postman. The error is not useful and I couldn't figure out what the problem is. I'm using multer as a memory storage middleware before uploading the file to google storage.

I went through many threads but I couldn't find a cure for my exact problem. This was the closest to my problem but non of the answers seemed to solve this issue for me: Unexpected end of form error when using Multer

Here is my express server:

require("../../bootstrap");

const project = process.argv[2];

const JSUtils = require("../api/utils/JSUtils");
const config = require(`../conf/${project}.json`);
const bodyParser = require("body-parser");

const express = require("express")();
const app = require("./App").default;
const toolkit = require("../toolkit/Toolkit").default;
const admin = require("firebase-admin");

admin.initializeApp({
    credential: admin.credential.cert(config),
    databaseURL: `https://${project}.firebaseio.com`,
});

express.use(bodyParser.json());
express.use("/*/us-central1/v1", app);
express.use("/*/us-central1/toolkit", toolkit);

express.listen(5000, () => {});
Enter fullscreen mode Exit fullscreen mode

Multer middleware:

export const upload = Multer({
    storage: Multer.memoryStorage(),
    limits: {
        fileSize: 10 * 1024 * 1024, // No larger than 10mb
        fieldSize: 10 * 1024 * 1024, // No larger than 10mb
    },
});
Enter fullscreen mode Exit fullscreen mode

Here is the express router I use for the file upload:

router.post(
    "/upload",
    UserMiddleware.checkUser(),
    upload.single("imgfile"),
    controller.handleFileUpload,
);
Enter fullscreen mode Exit fullscreen mode

controller:

export const handleFileUpload = async (req, res) => {
    const bucketName = `$google-storage-bucket`;


    console.log("request file", req.file);
    if (req.file) {
      //Upload file to google storage
     }
}
Enter fullscreen mode Exit fullscreen mode

And to so send the request in Postman I'm doing this:

Image description

I'm not adding the multipart/form-data content type manually as I know it is being set by postman automatically.

e API v1 - Caused by Error: Unexpected end of form
>      at Multipart._final (/Users/User/app-firebase/functions/node_modules/busboy/lib/types/multipart.js:588:17)
>      at callFinal (internal/streams/writable.js:610:10)
>      at processTicksAndRejections (internal/process/task_queues.js:82:21)
Enter fullscreen mode Exit fullscreen mode

I also tried eliminating the middlewares before the multer one but that didn't make a difference. I've also tried testing client-side and sending a fetch request with the created FormData similarly the response is the same.

Top comments (2)

Collapse
 
jacksonkasi profile image
Jackson Kasi
const config = {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    };

const res = await axios.post("/update-user-profile", data, config);
Enter fullscreen mode Exit fullscreen mode

for postman

try to add this content type

    "Content-Type": "multipart/form-data",
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tanya1705 profile image
Tanya Kumari

I am getting the exact error. Have you sorted it?