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

Error from body-parser when sent Content-Type header without body #771

Open
assapir opened this issue Aug 8, 2024 · 4 comments
Open

Error from body-parser when sent Content-Type header without body #771

assapir opened this issue Aug 8, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@assapir
Copy link

assapir commented Aug 8, 2024

What version of Elysia is running?

1.0.20

What platform is your computer?

Darwin 23.6.0 arm64 arm

What steps can reproduce the bug?

  1. Create a DELETE handler that does not expect a body, like:
.delete('/:id', async ({ article }) => {
  await deleteArticle(article!.id);
  return {
    sessionId: article!.id,
  };
});
  1. Send a request with a header of Content-Type: application/json

What is the expected behavior?

No failure, the handler will just be called

What do you see instead?

Failed to parse body as found: undefined is returned

Additional information

I think the fix should be - don't run the body parser if there is no content (the content-length header is absence or set to 0)

Have you try removing the node_modules and bun.lockb and try again yet?

yes, but when upgraded from 1.0.20 to 1.1.5 I always get Not Found on my routes, so I can't upgrade

@assapir assapir added the bug Something isn't working label Aug 8, 2024
@kristjansuursoho
Copy link

kristjansuursoho commented Aug 10, 2024

Quick fix

const elysia = new Elysia()
  .onParse(async ({ request, contentType }) => {
    try {
      if (contentType === 'application/json') {
        return await request.json()
      }
    } catch (error) {
      return request.text()
    }
  })

@kravetsone
Copy link
Contributor

What version of Elysia is running?

1.0.20

What platform is your computer?

Darwin 23.6.0 arm64 arm

What steps can reproduce the bug?

  1. Create a DELETE handler that does not expect a body, like:
.delete('/:id', async ({ article }) => {
  await deleteArticle(article!.id);
  return {
    sessionId: article!.id,
  };
});
  1. Send a request with a header of Content-Type: application/json

What is the expected behavior?

No failure, the handler will just be called

What do you see instead?

Failed to parse body as found: undefined is returned

Additional information

I think the fix should be - don't run the body parser if there is no content (the content-length header is absence or set to 0)

Have you try removing the node_modules and bun.lockb and try again yet?

yes, but when upgraded from 1.0.20 to 1.1.5 I always get Not Found on my routes, so I can't upgrade

U shouldn't lie server about content-type)

@kravetsone
Copy link
Contributor

But elysia can also check content-length

@on1force
Copy link

you can try to use the schema and just pass on t.Optional on the body, haven't tried this though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants