Multer upload fails with 'Unexpected end of form'
Error: Unexpected end of formShort 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.
Fix it fast
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));2Match what you see, then jump to its fix
- 1
The logged header is 'multipart/form-data' with no 'boundary=' partThe Content-Type header was set by hand and has no matching boundaryFix - 2
The 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
- 1
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
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 nginxConfirmed by nginx.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Back end
Uploaded files are missing from the request in Express
MulterError: LIMIT_UNEXPECTED_FILE
Express does not parse file uploads itself, and Multer only handles multipart/form-data.
4 causes, each with a fixBack end
Express rejects a request body as too large
413 (Request Entity Too Large) returned by nginx in front of the app
A 413 means a body was bigger than a size limit.
3 causes, each with a fixBack end
Socket.IO answers 404 on the polling request
Cannot GET /socket.io/
A 404 on /socket.io/?EIO=4&transport=polling means the request is not reaching a Socket.IO server.
3 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fix