07 · Applications
CBOR — binary request and response bodies
Read and write application/cbor. c.body() accepts a Uint8Array directly so you can return raw binary without encoding tricks.
Code
cbor.js
const { WebApp } = require(class="tk-str">39;@hyperbridge/forge/server39;); const { encode, decode } = require(class="tk-str">39;cbor-x39;); class="tk-cm">// any CBOR lib const app = new WebApp(); app.post(class="tk-str">39;/data39;, async (c) => { const buf = Buffer.from(await c.req.arrayBuffer()); const body = decode(buf); const out = encode({ echo: body, ts: Date.now() }); return c.body(out, 200, { class="tk-str">39;content-type39;: class="tk-str">39;application/cbor39;, }); }); app.listen(3000);
How it works
c.req.arrayBuffer() hands you the raw bytes; c.body(buf, status, headers) sends raw bytes back. No JSON detour.
Same pattern works for Protocol Buffers, MessagePack, Avro, or any custom binary protocol.
Body cache means you can call arrayBuffer() and still call text() later for logging — both read from the same memoized buffer.
Try it
Quickstart
client → CBOR-encoded body → server decodes, echoes, encodes back
Related modules
This is HBForge's port of Hono's example. Read the original at hono.dev/examples/cbor.