Trouble setting up oRPC with Hono on Bun — input validation fails with undefined input #905
-
I’m trying to set up oRPC with the Hono framework running on Bun. The main issue is that oRPC input validation fails, complaining that the input is `undefined` even though the JSON body is correctly sent and parsed by Hono.
---
**Setup**
- Using `@orpc/[email protected]`
- Using `[email protected]`
- Running on `bun@latest`
- Following the official oRPC Hono adapter docs: https://orpc.unnoq.com/docs/adapters/hono
---
**What I’ve tried**
- Using a proxy on `c.req.raw` that overrides `.json()` to forward to `c.req.json()`
- Ensuring the body stream is only consumed once inside the proxy
- Debug logging confirms that `c.req.json()` correctly parses the JSON body before it reaches oRPC
- Still, oRPC validation fails with:
```json
{
"code": "BAD_REQUEST",
"message": "Input validation failed",
"data": {
"issues": [
{
"expected": "object",
"message": "Invalid input: expected object, received undefined"
}
]
}
} Questions
Sample code import "dotenv/config";
import { RPCHandler } from "@orpc/server/fetch";
import { createContext } from "./lib/context";
import { appRouter } from "@/routers";
import { Hono } from "hono";
import { cors } from "hono/cors";
import { logger } from "hono/logger";
const app = new Hono();
app.use(logger());
app.use(
"/*",
cors({
origin: process.env.CORS_ORIGIN || "",
allowMethods: ["GET", "POST", "OPTIONS"],
})
);
const handler = new RPCHandler(appRouter);
app.use("/rpc/*", async (c, next) => {
const context = await createContext({ context: c });
const BODY_PARSER_METHODS = new Set(['arrayBuffer', 'blob', 'formData', 'json', 'text'] as const);
type BodyParserMethod = typeof BODY_PARSER_METHODS extends Set<infer T> ? T : never;
const request = new Proxy(c.req.raw, {
get(target, prop) {
if (BODY_PARSER_METHODS.has(prop as BodyParserMethod)) {
return () => c.req[prop as BodyParserMethod]();
}
return Reflect.get(target, prop, target);
}
});
const { matched, response } = await handler.handle(request, {
prefix: "/rpc",
context,
});
if (matched) {
return c.newResponse(response.body, response);
}
await next();
});
app.get("/", (c) => {
return c.text("OK");
});
import { serve } from "@hono/node-server";
serve(
{
fetch: app.fetch,
port: 3000,
},
(info) => {
console.log(`Server is running on http://localhost:${info.port}`);
}
); import { publicProcedure } from "../lib/orpc";
import type { RouterClient } from "@orpc/server";
import {z} from "zod";
export const appRouter = {
healthCheck: publicProcedure.handler(() => {
return "OK";
}),
testInput: publicProcedure
.input(z.object({
name: z.string(),
email: z.string()
}))
.handler(({ input }) => {
console.log("🧪 TestInput handler called!");
console.log("🧪 Input received:", input);
return {
success: true,
receivedInput: input
};
}),
};
export type AppRouter = typeof appRouter;
export type AppRouterClient = RouterClient<typeof appRouter>; Thank you in advance for any guidance or example code! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @andersonkxiass! I'm Dosu and I’m helping the orpc team. The proxy pattern you’re using to forward body parser methods from However, the input validation error (
There are no Bun-specific caveats or extra requirements documented for this integration—just ensure you use the proxy pattern for body parsing and the correct handler for your use case. If you want a minimal setup that works for manual API calls, swap To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Hi @andersonkxiass! I'm Dosu and I’m helping the orpc team.
The proxy pattern you’re using to forward body parser methods from
c.req.raw
to Hono’sc.req
is the officially recommended way to integrate oRPC with Hono, including on Bun. This approach is documented in the official oRPC Hono adapter docs.However, the input validation error (
expected object, received undefined
) is a known issue when usingRPCHandler
for direct/manual API calls (such as from API clients or tools). TheRPCHandler
is intended for use with oRPC’s generated clients, not for manual HTTP requests. To avoid this error, you have two options: