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";
request (required)RequestPromise<any>typescript
import { decodeRequestBody } from "@arkyn/server";// 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 requesttypescript
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.