I'm using a Vercel Serverless function to handle a file upload to Digital Ocean Spaces (identical API to AWS S3). However, I am running into issues handling multipart/form-data in the request handler.
On the front-end I'm using fetch to post a FormData() object with files and a couple of text fields. When logging the body and servers on the header, I can see everything there as expected, however when handling the multipart with Multer (I have also tried a couple of other similar packages) I am not able to retrieve any of the posted fields or files.
Also worth noting is that when using Postman to test the POST requests I'm running into the exact same issue, so I'm confident the issue lies in the serverless function.
Front-end:
const handleSubmit = async (values) => {
const formData = new FormData();
// build my Form Data from state.
Object.keys(values).forEach(key => {
formData.append(key, values[key]);
});
const response = await fetch("/api/post-submission", {
method: "POST",
headers: {
Accept: "application/json",
},
body: formData,
});
const json = await response.json();
};
Serverless handler:
const util = require("util");
const multer = require("multer");
module.exports = async (req, res) => {
await util.promisify(multer().any())(req, res);
console.log("req.body", req.body); // >> req.body [Object: null prototype] {}
console.log("req.files", req.files); // >> req.files []
// Do the file upload to S3...
res.status(200).json({ uploadData });
};
Expected behavior:
req.body and req.files should be populated with my submitted data.