Skip to content

Body already used error in v1.4.19 when accessing request.body or request.arrayBuffer() #1628

@fidaay

Description

@fidaay

What version of Elysia is running?

elysia@1.4.19

What platform is your computer?

Microsoft Windows NT 10.0.26200.0 x64

What environment are you using

Bun version: 1.3.5

Are you using dynamic mode?

No, I am not using new Elysia({ aot: false }).

What steps can reproduce the bug?

Steps:

  • Create a new Elysia app with version 1.4.19
  • Add a plugin using .derive() that attempts to read request.arrayBuffer() or access request.body
  • Make a POST request with a JSON body
  • Observe the error
import { Elysia } from 'elysia';

const rawBodyPlugin = new Elysia({ name: 'raw-body' })
  .derive(async ({ request }) => {
    let rawBody: ArrayBuffer | undefined;

    if (request.method !== 'GET' && request.method !== 'HEAD') {
      try {
        rawBody = await request.arrayBuffer();
      } catch (err) {
        console.error('Failed to capture body:', err);
        rawBody = undefined;
      }
    }
    return { rawBody };
  })
  .as('scoped');

const app = new Elysia()
  .use(rawBodyPlugin) // First plugin in the chain
  .post('/test', ({ rawBody }) => {
    return { bodySize: rawBody?.byteLength ?? 0 };
  })
  .listen(3000);

What is the expected behavior?

In version 1.4.18 and earlier, it's possible to be able to access request.arrayBuffer() or request.body without issues, since nothing has consumed the body stream yet.

The body should be readable by the first plugin that attempts to access it.

What do you see instead?

TypeError: Body already used
 code: "ERR_BODY_ALREADY_USED"
      at <anonymous> (src/plugins/raw-body.plugin.ts:15:33)
      at <anonymous> (src/plugins/raw-body.plugin.ts:9:11)
      at handle (node_modules/elysia/dist/bun/index.js:34:33)

Additional information

  • Workaround: Downgrade to elysia@1.4.18 - the bug does not occur in this version.
  • This is critical for API Gateway use cases where the body must be forwarded to upstream services.
  • Using request.clone() before reading also fails with the same error, indicating the body is consumed at the framework level before user code runs.
  • Setting parse: [] on routes to disable body parsing does not prevent the issue.

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

Yes, I ran rm -rf node_modules && rm bun.lockb && bun install and the issue persists with v1.4.19. Downgrading to v1.4.18 is the only working solution.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions