3

My Express application is returning "Request Entity Too Large" on a file upload of only a 125kb PNG file.

I have configured the body parser middleware as such:

app.use(bodyParser.urlencoded({
  limit: '5mb',
  type:'*/x-www-form-urlencoded',
  extended: true
}));

according to the documentation. No matter how high I set the limit, or what combination of options, I always get the same result. I am using Express 4.13.3 and body-parser 1.15.2.

What am I doing wrong?

Brandon
  • 29,908
  • 11
  • 86
  • 125
  • Many possible answers here, did you give them all a try? http://stackoverflow.com/questions/19917401/node-js-express-request-entity-too-large – Stéphane Bruckert Sep 25 '16 at 14:35
  • Hi @StéphaneBruckert Yes, I've done quite a bit of research. I've changed the order of the parsers, I've removed the json body parser and only used the urlecoded parser. I've tried adding the `parameterLimit` option... none of the answers that are working for others seems to be working for me. – Brandon Sep 25 '16 at 14:42
  • Are you uploading the file as Base64 or something? Because `body-parser` doesn't handle (proper) file uploads at all. – robertklep Sep 25 '16 at 17:29
  • @robertklep No, as a multi-part form using Postman, setting a `Content-Type` header of `application/x-www-form-urlencoded` and then setting a `form-data` body with a "photo" field and selecting a PNG file. – Brandon Sep 25 '16 at 19:20
  • I've tried skipping the `body-parser` and using multer middleware, but I get the same response. – Brandon Sep 25 '16 at 19:20
  • Multipart form data can't be uploaded using `application/x-www-form-urlencoded` (it should use `multipart/form-data`), perhaps that's the problem. So dont' set the `Content-Type` and use `multer`. Also, I assume that you're not using any other middleware that could be causing that response. – robertklep Sep 25 '16 at 19:36
  • @robertklep Well, this is embarrassing, but I had my route handler pointed to the wrong router (they are very close in name). Now I have a different error - the multer handler isn't picking up the file. – Brandon Sep 25 '16 at 21:43
  • @Brandon not even with the correct content-type header? – robertklep Sep 26 '16 at 05:56
  • It seems that Postman sends an array of files, even if you only send one, so `request.file` was `undefined`, but `request.files` was populated. – Brandon Sep 26 '16 at 10:17

1 Answers1

1

Embarrassingly, I had accidentally pointed my route handler to the wrong Express Router instance. Once I was pointed to the correct handler, Multer picked up the multi-part POST correctly.

Interestingly, when testing with Postman, it will send an array of files, even if you only select one, which is why request.file was undefined, but request.files contained the correct value.

Brandon
  • 29,908
  • 11
  • 86
  • 125