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, () => {});
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
},
});
Here is the express router I use for the file upload:
router.post(
"/upload",
UserMiddleware.checkUser(),
upload.single("imgfile"),
controller.handleFileUpload,
);
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
}
}
And to so send the request in Postman I'm doing this:
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)
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)
for postman
try to add this
content typeI am getting the exact error. Have you sorted it?