MultipartParser.end(): stream ended unexpectedly

9k views Asked by At

I'm trying to make a form with option for image upload. I'm using express-http-proxy as my API proxy and multer as suggested.

app.use('/api', upload.any(), proxy('http://localhost:3333'));

Issue is this error when submitting the form:

Error: MultipartParser.end(): stream ended unexpectedly: state = START_BOUNDARY at MultipartParser.end (/home/gabriel/Sites/city-amazing/api/node_modules/formidable/lib/multipart_parser.js:326:12) at IncomingMessage. (/home/gabriel/Sites/city-amazing/api/node_modules/formidable/lib/incoming_form.js:130:30) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:975:12) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickCallback (internal/process/next_tick.js:98:9)

How to handle any file upload using express?

4

There are 4 answers

0
Omar Faruque Sohag On

If you are using postman please uncheck the content-type

0
Jayden On

I got the same issue and fixed it by setting parseReqBody: false

app.use('/post', proxy('httpbin.org', {
  parseReqBody: false
}));
2
Dhiraj Narsinghani On

I had the same error at my node + express back-end while accepting the image.

  1. The first error I had was weak header, multipart/form-data without boundary.
  2. Then after giving a boundary to it, I faced this same error MultipartParser.end(): stream ended unexpectedly

I spent almost 5 hours debugging these errors and found out that don't give content-type header while sending your data. You may use other headers, but don't use content-type: multipart/form-data while sending formData to back-end.

This worked for me in my MERN project. It should work with other back-ends too.

1
Yashas On

I got this error when I was trying to parse an image file from a form (multipart-form-data). The library I used for parsing is called "formidable". So, i see that the same library is causing the error here as well.

The reason I got this error was because i set the encoding type for my request object this way request.setEncoding("utf8");. And then I was passing the request object to the parse method of formidable this way, new formidable.IncomingForm().parse(request, callback); This is what caused the error for me.

I simply removed the line request.setEncoding("utf8"); and it started working.