Matthew C.
—When using Multer, which is a node.js middleware for handling multipart/form-data
, you may encounter the following error when trying to upload a file from a form:
Error: Unexpected end of form
The error occurs during parsing the form data, which is done by one of Multer’s dependencies, busboy
. The error indicates that the form data is incomplete or not structured correctly. For example, it may be missing the final closing boundary that separates the different parts of the multi-part form data.
The error may occur when using more than one npm library to upload files. For example, if you use Multer and express-fileupload
in the same API route:
app.use(fileUpload()); const upload = multer({ dest: "uploads/" }); app.post( "/profile", upload.single("avatar"), (req, res, next) => { res.send("File uploaded successfully!"); }, (error, req, res, next) => { console.log(error.message); res.status(500).send({ error: error.message }); }, );
When posting data to this endpoint, you need to pass in multi-part form data. For example, the submission <form>
tag needs the "multipart/form-data"
enctype
attribute added to it and the method type must be "post"
for sending files:
<form action="/profile" method="post" enctype="multipart/form-data"> <input type="file" name="avatar" /> <button type="submit">Submit</button> </form>
The enctype
attribute specifies the encoding type the browser should use when sending the data to the server. When set to multipart/form-data
, the form data is split into multiple parts, one for each file and one for any text input data in the form.
In the above API route the fileUpload()
method of express-fileupload
, which also depends on busboy
, parses the request and alters it. The file
property of the req
object is undefined
and the file data is added to the files
property of the req
object. Multer attempts to parse the parsed stream, causing the error.
Only use one library or middleware for file uploads, or only use one per API route:
const upload = multer({ dest: "uploads/" }); app.post( "/profile", upload.single("avatar"), (req, res, next) => { res.send("File uploaded successfully!"); }, (error, req, res, next) => { console.log(error.message); res.status(500).send({ error: error.message }); }, );
Also check that none of your middleware, such as body-parser, alters the incoming multi-part form data request unexpectedly.
You may also get this error if you use Multer inside a cloud function, such as Google Cloud or Firebase functions. The Multer dependency busboy
calls the following to stream in the request body for processing:
req.pipe(busboy)
Google Cloud functions pre-stream the entire request body into a custom res.rawBody
property that causes a parsing error in Multer. There’s an open Multer GitHub issue for this error: Add configuration parameter to support Google Cloud Functions.
Alternatively, you can use the lower-level busboy
library instead of Multer to fix this issue. Replace req.pipe(busboy)
with busboy.end(req.rawBody)
when using cloud functions as discussed in this Busboy GitHub issue.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.