Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Multer upload fails with 'Unexpected end of form'

ErrorError: Unexpected end of form

Short answer

Multer reads multipart bodies with busboy, which raises this error when the request body ends before the closing boundary line. Two causes cover most cases: a hand-set Content-Type header whose boundary does not match the body, or a request cut short on the way, for example by a proxy timeout.

  • Applies to Multer 2, Express 5, Node.js 22, nginx 1.26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the Content-Type header the server actually receives, and compare the size you sent with what arrived. A header without a boundary, or a body shorter than Content-Length, points to the cause.

    app.post('/upload', (req, res, next) => {
      console.log(req.headers['content-type'], req.headers['content-length']);
      next();
    }, upload.single('file'), (req, res) => res.sendStatus(200));
  2. 2Match what you see, then jump to its fix

    1. 1The logged header is 'multipart/form-data' with no 'boundary=' partThe Content-Type header was set by hand and has no matching boundaryFix
    2. 2The error appears only on large files or slow connections, and the nginx error log shows a 408 or a closed connectionThe request body was cut short before the final boundaryFix

What does the error look like?

  • Error: Unexpected end of form

Cause 1 of 2

The Content-Type header was set by hand and has no matching boundary

1Check

A multipart body is split into parts by a boundary string, and the Content-Type header must name that same boundary. When the header is typed in manually, it replaces the generated one. Postman states that a manually selected Content-Type takes precedence over what it sets.

curl -i -X POST http://localhost:3000/upload \
  -F "file=@./photo.jpg"

2Fix

Do not set Content-Type yourself when you send form-data. In Postman, untick or delete the manual Content-Type header. In a browser or a client library, pass the FormData object and leave the header out so the boundary is generated for you.

const form = new FormData();
form.append('file', fileInput.files[0]);
// no headers: { 'Content-Type': ... } here
await fetch('/upload', { method: 'POST', body: form });

3Verify

The curl form above, which builds the header itself, returns 200, and so does your client.

curl -i -X POST http://localhost:3000/upload -F "file=@./photo.jpg"

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 2

The request body was cut short before the final boundary

1Check

busboy reports this error when the stream ends before it has seen the closing boundary. With nginx in front, client_body_timeout (60 seconds by default) ends a request if the client sends nothing for that long between two reads.

grep -R "client_body_timeout\|client_max_body_size" /etc/nginx/
sudo tail -n 30 /var/log/nginx/access.log | grep " 408 "

2Fix

Raise client_body_timeout for the upload route. If you also see 413 responses, raise client_max_body_size for the same location.

location /upload {
    client_body_timeout 120s;
    client_max_body_size 50m;
    proxy_pass http://127.0.0.1:3000;
}

3Verify

Reload nginx and upload the same large file again.

sudo nginx -t && sudo systemctl reload nginx

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • Never type a multipart Content-Type header by hand. Let the client generate it with the boundary.
  • Give upload routes their own nginx location with body size and timeout limits that fit your largest expected file.
  • Handle errors from Multer in an error-handling middleware, so a failed upload returns a clear 400 instead of a crash.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.