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
Automatic decoding of JSON fields doesn't work well with browser requests #292
Comments
I've opened the following |
Hi @cyantree - I was trying to reproduce your use case. If you look closely to the documentation you'll see that the way you are trying to get the contents of the body is wrong. I wrote a simple example on how you should access the field(s) ( const fastify = require('fastify')()
fastify.register(require('./index', {
attachFieldsToBody: true,
}))
fastify.post('/test', async (req, rep) => {
const data = await req.file()
rep.send(data.fields.json.file) // will return to the browser a FileStream containing: {"foo": "bar"}
});
fastify.listen(3000, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
}) Since all the fields are considered "files" you have to read them as such, in your server. I hope this helps. |
Hi @radomird,
In my usecase I want to use the functionality described here:
So since 5.1 it should be possible to pass data as a non-file field that will be parsed and validated automatically. The following request would be processed correctly - but only because the part doesn't contain a filename:
In this case A full working example is:
However I found a new workaround that doesn't add too much overhead to get it running:
By manually converting the JSON-data into a non-file field the data still will be validated through the configured schema. |
Could @cyantree explain why you need to use the
I don't think busboy will accept the request because the |
Hi @Eomm, My use case is a browser upload form which also passes some additional data. I want to use the functionality provided by this package for file and data validation. I'm using a Blob because this allows me setting the Does your example work for you? It just throws an error for me:
I assume you're running your example on Node.js with Yes, |
@cyantree After looking into this issue once more, the conclusion is that the workaround that you posted earlier is great, if it solves your problem then I think we are good. Despite the issue with This can be solved in two ways:
|
An alternative idea is to remove the automatic decoding of JSON fields because we can't support it. I don't think there is a brtter alternative than busboy. |
Thanks @radomird and @mcollina for your time. @radomird @mcollina Regarding switching libraries: |
@mcollina Why not fork busboy to fastify org and do fixes there? Isn't that the route we've taken in the past? |
|
I propose we revert #288 as it was a recent addition. This feature was asked recently I think it requires too much work to support. (I'm ok in forking, but I think it's a stretch here.) |
@mcollina Considering that busboy is pretty much unsupported right now, wouldn't that make sense long-term? And it would allow fixing this particular issue properly. |
I'm ok if somebody else would help but I have no bandwidth atm. |
I can do this. |
@mcollina I've created https://github.com/fastify/busboy and will work over weekend to update it a bit and merge whatever outstanding PRs make sense. After first forked version is ready to go, I'll open PR in fastify-multipart to do the switch. |
@kibertoad Yep, sure. Thanks!
|
@cyantree Let's continue discussing in fastify/busboy repo (probably we need to create a new issue there), but I would go with approach 1 - I don't think we need to maintain backwards compatibility in this case, I would treat first release of fastify/busboy as a breaking change either way. |
After #300 lands, we should revisit this issue and check if fastify-multipart exposes enough of busboy configuration to make use of new isPartFile config option, or if some extra changes would be needed. |
@cyantree Can you check if this is possible on latest master without any additional changes to fastify-multipart? |
@kibertoad
However with TS it doesn't work without fixing the types as |
@cyantree Don't we just proxy BusboyConfig types in fastify-multipart? which part misses the type? |
I'm using it like this:
In my installed version
I've installed it in I can confirm that it works within a handler like this:
However here I'm expected to provide |
I'll check if having a subset of busboy config is ok there. |
@cyantree Can you check with 5.2.1? I think I added missing type in the right place :) |
Thanks @kibertoad . It works for me. :) |
Prerequisites
Fastify version
3.22.1
Plugin version
5.1.0
Node.js version
16.3.0
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
20H2
Description
fastify-multipart
usesbusboy
for multipart parsing.busboy
has a logic to decide whether a part is a field or file by checking the filename:https://github.com/mscdex/busboy/blob/master/lib/types/multipart.js#L174
However this doesn't work well with browser generated requests as it doesn't seem possible to leave out the filename when specifying a content type. The best I've achieved is passing an empty filename.
Steps to Reproduce
Setup the server:
Define the route:
Send a browser request:
Expected Behavior
The expected behaviour is getting a console output of the json payload.
However because of
busboy
's behaviouronField
won't be triggered and therefore json parsing not started:https://github.com/fastify/fastify-multipart/blob/master/index.js#L358
A possible solution could be checking detected files whether they have an empty filename and then deciding whether to parse them as json.
Another (hacky) solution would be allowing to configure a naming schema for part names that would be used to decide whether to handle a part as file or json field. Part names could look like
json.foo
.However the best solution would be to adjust
busboy
. However I'm not sure whether this is realistic as it hasn't been updated in years.The text was updated successfully, but these errors were encountered: