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

Using json() and parseBody() same times #3881

Open
Ily1606 opened this issue Feb 1, 2025 · 2 comments
Open

Using json() and parseBody() same times #3881

Ily1606 opened this issue Feb 1, 2025 · 2 comments
Labels

Comments

@Ily1606
Copy link

Ily1606 commented Feb 1, 2025

What version of Hono are you using?

4.6.18

What runtime/platform is your app running on? (with version if possible)

Nodejs

What steps can reproduce the bug?

RequestKernel.ts class

public static async processData(request: HonoRequest) {
  request.input = await Promise.allSettled([
    request.json(),
    request.parseBody({
      dot: true,
    })
  ]).
  then((dataProcessed) => {
    return dataProcessed.reduce((carry, item) => {
      if(item.status === 'fulfilled') {
        return {
          ...carry,
          ...item.value
        }
      }

      return carry;
    }, {
      ...request.query()
    })
  })

  return request;
}

Request.test.ts

const rawRequest = new Request(getUrl(routeMock.path), {
  method: "POST",
  body: createFormData({
    email: '[email protected]',
    password: 'password123',
    name: 'John Doe',
  })
})
const request = new HonoRequest(rawRequest);
const requestInstance = await RequestKernel.processData(request);

What is the expected behavior?

request.input should be

{
  email: '[email protected]',
  password: 'password123',
  name: 'John Doe',
}

(Parsed from FormData, json is error will be skipped)

What do you see instead?

Inside allSettled().then()
I got Array<PromiseResult> with a same error:

"No number after minus sign in JSON at position 1"

But I'm using parseBody(), json() as sequence, it's working

const test = request.parseBody({
  dot: true,
}) // working

request.input = await Promise.allSettled([
  request.json(),
])

Additional information

I'm using Nodejs v20.17.0

@Ily1606 Ily1606 added the triage label Feb 1, 2025
@Ily1606 Ily1606 changed the title Parse json() and parseBody() same times Using json() and parseBody() same times Feb 1, 2025
@Ily1606
Copy link
Author

Ily1606 commented Feb 1, 2025

I see #cachedBody() in the following link:

return (bodyCache[key] = raw[key]())

It is used as a "computed" function to get and set data in bodyCache.

In my case, I am posting FormData, but when I try to parse it into JSON, the logic of #cachedBody() always returns the first result of the parsing process, including any parsing errors.

@yusukebe
Copy link
Member

yusukebe commented Feb 2, 2025

@lmcarreiro

Can you provide a minimal project to reproduce it?

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

No branches or pull requests

2 participants