multer
multer copied to clipboard
Custom middleware request body gets overriden
I have custom middleware that appends request body and puts some value there. But when is form-data, that appended value dissapears from request body. I did a bit of investigation around and i found why.
In multer file lib/make-middleware.js at line 27, there is this piece of code req.body = Object.create(null) which destroys previous set values by my middleware. I simply commented this and solved problem for me. I didn't go further to investigate why this piece of code exists and purpose of it.
Hmm, what is the use case for adding anything to req.body, I'm guessing that that middleware isn't actually reading from the body of the request since that would consume the stream and multer wouldn't be able to operate.
The reason we are setting req.body is that we put the values that we parse from the formdata (sans files) there
Im using tokens to recognize users then to inject user_id into req.body. Everything works well, just in multipart req.body created from my middleware is overriden by multers.
I would recommend putting it onto req.user instead since it's semantically wrong to put it on req.body since it didn't come from the body... This is for example how passport.js does it, a very popular authentication library
Thanks for recommendation, anyway, i just wanted to see is there any special reason to override req.body set by any other middleware.
Well, we are setting the req.body property because multipart uploads can contain both fields and files. The files will end up in req.files and the fields in req.body. This is how all the body-parsers works as far as I know...
Maybe you could place your middleware after multer?
in any case it wouldn't harm to extend the existing .body with your body instead of overwriting. if the fields with same name appear in the form-data request - they will be overwritten as before, isn't it?
Any news on this issue?
@kikar what is your use case?
I still don't think it's semantically correct to extend the body property since we are the ones parsing the body 🤔
I still don't think it's semantically correct to extend the body property since we are the ones parsing the body 🤔
maybe a config setting is in order? like bodyComposition: "extend"/"construct" where "construct" is default. You'll both retain compatibility and solve this problem.
Yes. I'm facing the similar problem. I'm setting Audit Columns (CreatedBy, CreatedDate, ModifiedBy, ModifiedDate) in body using a middleware. When uploading a file, this data gets missing. Extending the body would be better in such cases.
Thanks you just save my day ! Why would multer override my req body I don't get it, if I have a form which contains both file and extra fields the file will upload but the rest of my form will disappear which is just weird.
This is really frustrating, I am coding on a codebase that extensively uses request body manipulation and it totally works fine but adding multer destroys everything as at authentication needs to be done before file upload and uses req.body which multer destroys...
I am facing the same issue, I want to authenticate the user before passing the request through multer middleware but multer just overrides the body of my request. I think it would be better to extend the old body instead of creating new.
Someone can submit a PR for this and the discussion can continue there. Seeing an implementation as a PR would give this a chance to make it into the next major version.
Hi @jonchurch , I have created a PR, could you please review it.
@LinusU Or better you could provide some other option like callback or something where we can provide multer values we want to append to the request body before multer nukes it?
Any news? From what I saw the pr hasn't been merged yet
I would recommend putting it onto
req.userinstead since it's semantically wrong to put it onreq.bodysince it didn't come from the body... This is for example how passport.js does it, a very popular authentication library @LinusU Hey thanks this worked.