Skip to content

TypeError: Body is unusable #4259

Closed
@piotrgrundas

Description

@piotrgrundas

What version of Hono are you using?

4.8.3

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

Node

What steps can reproduce the bug?

Add 2x zValidators for path/middleware for json, header and try to access raw body text in route handler.

From my investigations, this is causing the error. If we clone the json using await c.req.raw.clone().json(), it works fine.

Example using create hono app:

import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { z } from "zod";
import { zValidator } from "@hono/zod-validator";

const schema = z.object({  mock: z.string() });

const app = new Hono();

app
  .get("/", (c) => {
    return c.text("Hello Hono!");
  })
  .post(
    "/",
    zValidator("header", schema),
    zValidator("json", schema),
    async (c) => {
      // causes error
      console.log(await c.req.raw.clone().text());

      return c.text("Hello Hono!");
    }
  );

serve(
  {
    fetch: app.fetch,
    port: 3000,
  },
  (info) => {
    console.log(`Server is running on http://localhost:${info.port}`);
  }
);

What is the expected behavior?

I'm able to use multiple zValidators - especially for json & header - and still access the raw body.

What do you see instead?

TypeError: unusable
    at Request.clone (node:internal/deps/undici/undici:9942:17)
    at Request.value (file:///Users/pete/Development/poc/hono-bug/node_modules/.pnpm/@hono+node-server@1.14.4_hono@4.8.3/node_modules/@hono/node-server/dist/index.mjs:122:40)
    at <anonymous> (/Users/pete/Development/poc/hono-bug/src/index.ts:21:35)
    at dispatch (file:///Users/pete/Development/poc/hono-bug/node_modules/.pnpm/hono@4.8.3/node_modules/hono/dist/compose.js:22:23)
    at file:///Users/pete/Development/poc/hono-bug/node_modules/.pnpm/hono@4.8.3/node_modules/hono/dist/compose.js:22:46
    at file:///Users/pete/Development/poc/hono-bug/node_modules/.pnpm/hono@4.8.3/node_modules/hono/dist/validator/validator.js:81:11
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async dispatch (file:///Users/pete/Development/poc/hono-bug/node_modules/.pnpm/hono@4.8.3/node_modules/hono/dist/compose.js:22:17)
    at async file:///Users/pete/Development/poc/hono-bug/node_modules/.pnpm/hono@4.8.3/node_modules/hono/dist/validator/validator.js:81:5
    at async dispatch (file:///Users/pete/Development/poc/hono-bug/node_modules/.pnpm/hono@4.8.3/node_modules/hono/dist/compose.js:22:17)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions