Registry / web-framework / koa-body

koa-body

JSON →
library7.0.1jsnpmunverified

koa-body is a robust middleware for the Koa.js web framework designed to parse various request body types. It supports `multipart/form-data` for file uploads, `application/x-www-form-urlencoded`, and `application/json` payloads. Functionally, it offers similar capabilities to a combination of Express's `bodyParser` and `multer`. The current stable version is 7.0.1, and the project is actively maintained by the Koa community, demonstrating a healthy release cadence with recent updates in 2025. Key differentiators include its seamless integration with Koa's middleware system, comprehensive content-type support, and flexible options for patching the parsed body to Koa's context (`ctx.request.body`) or Node's native request object (`ctx.req.body`), alongside configurable limits for body and file sizes.

web-frameworkserialization
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Since v7, `koaBody` is a named export. Default imports are not supported.

import { koaBody } from 'koa-body';

Even in CommonJS, `koaBody` is a named export, requiring destructuring.

const { koaBody } = require('koa-body');

TypeScript type import for configuring the middleware options.

import type { KoaBodyMiddlewareOptions } from 'koa-body';

This quickstart demonstrates setting up a Koa application with `koa-body` to parse JSON, urlencoded, and multipart request bodies. It logs the parsed body and any uploaded files, then responds with a confirmation message.

import Koa from 'koa'; import { koaBody } from 'koa-body'; const app = new Koa(); // Apply koa-body middleware globally or on specific routes app.use(koaBody({ multipart: true, // Enable multipart for file uploads urlencoded: true, json: true, formLimit: '1mb', // Limit form body size jsonLimit: '1mb', // Limit JSON body size textLimit: '1mb' // Limit text body size })); app.use(async (ctx) => { // For POST, PUT, PATCH requests, the parsed body is available at ctx.request.body if (ctx.method === 'POST' || ctx.method === 'PUT' || ctx.method === 'PATCH') { console.log('Request Body:', ctx.request.body); if (ctx.request.files) { console.log('Uploaded Files:', ctx.request.files); } ctx.body = `Received: ${JSON.stringify(ctx.request.body)}`; } else { ctx.body = 'Send a POST, PUT, or PATCH request with a body.'; } }); const port = 3000; app.listen(port, () => { console.log(`Koa server listening on http://localhost:${port}`); console.log('Try: curl -i http://localhost:3000/users -d "name=test" -X POST'); console.log('Or (for files): curl -i -X POST -F "name=filetest" -F "file=@./package.json" http://localhost:3000/upload'); });
Debug
Known footguns
gotchaThe v7.0.0 major version bump primarily introduced internal tooling updates, refactoring, and improved TypeScript support, rather than significant breaking API changes for most common use cases. While internal types and raw body access (`ctx.request.rawBody`) were refined, core API usage for parsing `json`, `urlencoded`, and `multipart` largely remained consistent.
breaking`koa-body` by default only parses bodies for `POST`, `PUT`, and `PATCH` HTTP methods. Other methods like `GET`, `HEAD`, or `DELETE` will not have `ctx.request.body` populated unless explicitly configured.
gotchaFor requests with unsupported text body types (e.g., `text/xml`), the raw unparsed body is available at `ctx.request.body` directly without needing `includeUnparsed` option, provided `koa-body` is used. If `includeUnparsed` is true, it also populates `ctx.request.rawBody` for non-multipart bodies.
gotchaFile uploads (multipart bodies) are disabled by default. If you intend to handle file uploads, you must explicitly enable the `multipart` option.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
15 hits · last 30 days
gptbot
4
ahrefsbot
3
script
1
googlebot
1
Resources