multer icon indicating copy to clipboard operation
multer copied to clipboard

Custom middleware request body gets overriden

Open kilgaloon opened this issue 8 years ago • 18 comments

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.

kilgaloon avatar Jan 17 '17 10:01 kilgaloon

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

LinusU avatar Jan 17 '17 12:01 LinusU

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.

kilgaloon avatar Jan 17 '17 19:01 kilgaloon

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

LinusU avatar Jan 18 '17 10:01 LinusU

Thanks for recommendation, anyway, i just wanted to see is there any special reason to override req.body set by any other middleware.

kilgaloon avatar Jan 18 '17 10:01 kilgaloon

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?

LinusU avatar Jan 18 '17 10:01 LinusU

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?

Spown avatar Feb 01 '17 09:02 Spown

Any news on this issue?

kikar avatar Aug 06 '18 12:08 kikar

@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 🤔

LinusU avatar Aug 06 '18 12:08 LinusU

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.

Spown avatar Aug 06 '18 12:08 Spown

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.

kgrvr avatar Aug 11 '18 14:08 kgrvr

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.

Hadard753 avatar Jun 17 '19 08:06 Hadard753

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...

reza7rm avatar Jul 20 '19 14:07 reza7rm

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.

prakharvk avatar Dec 10 '19 08:12 prakharvk

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.

jonchurch avatar Jan 26 '20 05:01 jonchurch

Hi @jonchurch , I have created a PR, could you please review it.

george3447 avatar Jun 08 '20 17:06 george3447

@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?

vinayk7 avatar Jun 12 '20 11:06 vinayk7

Any news? From what I saw the pr hasn't been merged yet

vandermnt avatar Dec 13 '23 02:12 vandermnt

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 @LinusU Hey thanks this worked.

Swapnil7711 avatar Dec 29 '23 07:12 Swapnil7711