HBForge/Examples
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.

forge/serverNodeBunDenoCF Workers
Code
cbor.js
const { WebApp } = require(class="tk-str">'@hyperbridge/forge/server');
const { encode, decode } = require(class="tk-str">'cbor-x');   class="tk-cm">// any CBOR lib
const app = new WebApp();

app.post(class="tk-str">'/data', 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">'content-type': class="tk-str">'application/cbor',
  });
});

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