Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add parseJsonFields option #1037

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

add parseJsonFields option #1037

wants to merge 6 commits into from

Conversation

panzi
Copy link

@panzi panzi commented Oct 1, 2021

Fields may also have a Content-Type header. If you set parseJsonFields to
true these fields will be parsed using JSON.parse() instead of handled as
plain text strings. This way you don't need to unroll complex JSON structures
that are transmitted alongside uploaded files as url-encoded fields.

If this is not good for some reason I'd at least like to have some way to get
what the content-type of the field was in the request handler, so I can at least
handle that situation manually.

A possible extension: Make it parseFields instead and not just handle
application/json content-types, but also fields with a
application/x-www-form-urlencoded content-type, or instead of a boolean
flag make it possible to pass a filter function (value = fieldFilter(value, mimetype)).
An advantage to the filter function approach would be the possibility to throw
a custom error type which can be handled more specifically than a SyntaxError
in the servers error handler.

@noureldin-khaled
Copy link

Hi @panzi 👋 any ideas of if/when will this be released? I'm currently facing this problem and stuck with no solution other than trying unroll the JSON structure I have which will be a nightmare.
Ideally like you said there would be a generic parseFields but I would settle for just the parseJsonFields now

@panzi
Copy link
Author

panzi commented May 14, 2022

@noureldin-khaled I don't know more than is documented here. I had this problem, so I forked multer to add this feature and made a pull request. Never heard anything back.

@LinusU
Copy link
Member

LinusU commented May 30, 2022

Sorry for not responding to this earlier!

I personally feel that this goes a bit beyond the intended scope of Multer. Is there anything that is preventing this to be made into a separate package, that wraps/extends Multer?

I'm also not familiar with any use case that would be solved by this option. Would you be able to share some use cases where this solution is a good fit?

@panzi
Copy link
Author

panzi commented May 30, 2022

If a field is sent as Content-Type: application/json I want to decode it correctly on the server side (versus when it is text/plain or has no header, where I handle it as a single string value). However, multer just drops the headers of fields and there is no way to handle that manually. Therefore I would like a feature where I could either supply a function to decode fields where the decoding function gets the mime type or a feature where mutler just decodes such fields automatically. Note that I'm talking about otherwise normal fields, not uploaded files. Fields that are supplied in addition to uploaded files.

@LinusU
Copy link
Member

LinusU commented May 31, 2022

Which client is sending you this data? Is this something that can be done from the browser?

It seems like this is almost like a request in the request, and potentially should be able to use any regular express body parser?

@panzi
Copy link
Author

panzi commented May 31, 2022

It should be possible to be done from the browser using FormData, though I do it with another node app as the client (which uses node fetch and FormData).

What express body parser can do this? Well, yes, express can parse JSON bodies, but not when part of a multi part request (file upload). That's what I use multer for and multer doesn't support it. Or is there a way to combine normal body parsers with multer? I haven't found a way to do that.

@noureldin-khaled
Copy link

@LinusU Kindly consider including this PR with the next release, since multer overrides body parser when the request is multipart I feel like it should support all scenarios, including the scenario when a json object is sent in the multipart request, it should correctly parse it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants