hono
hono copied to clipboard
req.parseBody should allow multiple keys (and converted to array)
Currently
Only the last key is recorded into the resulting object after req.parseBody()
.
Expected
When parsing request parameters that consists of multiple keys, the keys should be converted to array automatically.
tests=1&tests=2&tests=3
{ tests: [ 1,2,3 ] }
Also, another use case where using tests[]
which has been around for some time also need to be supported.
tests[]=1
{ tests: [ 1 ] }
Related: https://github.com/honojs/hono/issues/257
please see https://hono.dev/api/request
// Captured params
app.get('/entry/:id', (c) => {
const id = c.req.param('id')
...
})
// Get all params at once
app.get('/entry/:id/comment/:commentId', (c) => {
const { id, commentId } = c.req.param()
})
@bogordesaincom The one that's more relevant is this https://hono.dev/api/request#queries. Thanks for the hint.
ahh yes. that query section
this doesn't answer the question, as the question is clearly about req.parseBody()
.
I have run into this issue as well today. They are right that only the last value is parsed from the body.
Hi. We may fix it. But it will be a breaking change. So, will be included in the next major release.
I saw a similar issue which suggested manually parsing form data, so it’s not a priority for me. Thanks!
this doesn't answer the question, as the question is clearly about req.parseBody().
Because parseBody and queries may have the same format, I hope both enable single key multiple value parsing to array.
I think this implementation is good, if passing the all: true
as an option, it return multiple values as an array:
c.req.parseBody({ all: true })
With this way, it will not be a breaking change.
I ran into this as well.
I think it would be better to parse multiple keys into an array by default for consistency & predictability. { all: true }
is inconsistent with the way Hono handles queries. Parsing multiple keys into an array by default also makes it clear for users what to expect: post a key once if you want a single value and post it multiple times if you want an array.
The { all: true }
option also would mean that the validator & zValidator middleware would need this argument as well.
The PR #1488 may resolve this issue.
My concern is that the user expects an array to come in, but a single value comes in and it is not an array. This is not the behavior the user expects.
So I suggested { all: true }
. When using a validator, the user specifies whether it is an array or not, so the all
option is not necessary.
The same reason why FormData
has get()
and getAll()
.
https://developer.mozilla.org/en-US/docs/Web/API/FormData/get https://developer.mozilla.org/en-US/docs/Web/API/FormData/getAll
Nevertheless, it can be solved if the user checks if it’s an array or not.
app.post('/', async (c) => {
const { tests } = await c.req.parseBody()
if (Array.isArray(tests)) {
//...
} else {
//...
}
//
})
@yusukebe these small things would be nice tips on the docs 👍
how to use this with zod validator?
Hi @mrevanzak
Currently, the validator does not support array values in form
. We may make the validator support it, but it's not soon.
did it mention anywhere in the docs?