decodeRequestBody function is a utility for parsing incoming HTTP request bodies into JavaScript objects. It automatically detects and handles both JSON and URL-encoded form data formats, providing a unified interface for request body extraction.ts
import { decodeRequestBody } from "@arkyn/server/decodeRequestBody";
request (required)Requestoptions (optional){ maxBodySizeBytes?: number }maxBodySizeBytes: the maximum accepted body size, in bytes. Defaults to 5242880 (5 MB). Added in v3.0.10 so an unbounded request body can't be read and parsed in full before being rejected. Content-Length is checked first (fails fast without reading the body), and the actual size of the buffer read is checked again afterward, so a request with a missing or inaccurate Content-Length header is still caught. Exceeding the limit throws BadRequest before JSON.parse()/URLSearchParams parsing runs.Promise<any>typescript
import { decodeRequestBody } from "@arkyn/server/decodeRequestBody";// In a request handlerasync function handleRequest(request: Request) {const body = await decodeRequestBody(request);console.log(body);// Output: { name: "John", email: "john@example.com" }}
JSON.parse().= and attempts to parse it as URL-encoded form data using URLSearchParams.BadRequest error.BadRequest - Invalid URLSearchParams format= characters (indicating it's not URL-encoded form data).BadRequest - Failed to extract data from requestBadRequest - Request body too largeoptions.maxBodySizeBytes (default 5 MB), whether that's caught upfront from the Content-Length header or from the actual size of the body read.typescript
try {const data = await decodeRequestBody(request);} catch (error) {// BadRequest: Invalid URLSearchParams format}
arrayBuffer() and decodes it as UTF-8 text before attempting to parse. This approach ensures compatibility with various content encodings.Request object, making it compatible with modern runtimes like Bun, Deno, and Cloudflare Workers.