hono icon indicating copy to clipboard operation
hono copied to clipboard

req.parseBody should allow multiple keys (and converted to array)

Open Abdillah opened this issue 1 year ago • 15 comments

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

Abdillah avatar Mar 30 '23 03:03 Abdillah

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 avatar Apr 25 '23 14:04 bogordesaincom

@bogordesaincom The one that's more relevant is this https://hono.dev/api/request#queries. Thanks for the hint.

Abdillah avatar Apr 26 '23 06:04 Abdillah

ahh yes. that query section

bogordesaincom avatar Apr 28 '23 13:04 bogordesaincom

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.

RussellSanders1 avatar Sep 07 '23 22:09 RussellSanders1

Hi. We may fix it. But it will be a breaking change. So, will be included in the next major release.

yusukebe avatar Sep 07 '23 23:09 yusukebe

I saw a similar issue which suggested manually parsing form data, so it’s not a priority for me. Thanks!

RussellSanders1 avatar Sep 08 '23 00:09 RussellSanders1

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.

Abdillah avatar Sep 08 '23 00:09 Abdillah

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.

yusukebe avatar Sep 10 '23 12:09 yusukebe

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.

rimai4 avatar Sep 21 '23 06:09 rimai4

The PR #1488 may resolve this issue.

yusukebe avatar Sep 21 '23 21:09 yusukebe

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 avatar Sep 21 '23 22:09 yusukebe

@yusukebe these small things would be nice tips on the docs 👍

rafaell-lycan avatar Oct 01 '23 01:10 rafaell-lycan

how to use this with zod validator?

mrevanzak avatar Feb 27 '24 06:02 mrevanzak

Hi @mrevanzak

Currently, the validator does not support array values in form. We may make the validator support it, but it's not soon.

yusukebe avatar Feb 27 '24 16:02 yusukebe

did it mention anywhere in the docs?

muhaimincs avatar Jun 07 '24 05:06 muhaimincs