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
base: master
Are you sure you want to change the base?
Conversation
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. |
@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. |
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? |
If a field is sent as |
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? |
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. |
@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. |
Fields may also have a
Content-Type
header. If you setparseJsonFields
totrue
these fields will be parsed usingJSON.parse()
instead of handled asplain 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 handleapplication/json
content-types, but also fields with aapplication/x-www-form-urlencoded
content-type, or instead of a booleanflag 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.