Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@koa/bodyparser

koajs187.4kMIT6.0.0TypeScript support: included

Koa body parsing middleware

koa, body, request-body, bodyParser, json, urlencoded, text, xml

readme

@koa/bodyparser

NPM version build status Coveralls node version

Koa body parsing middleware, based on co-body. support json, form and text type body.

Parse incoming request bodies in a middleware before your handlers, available under the ctx.request.body property.

⚠ Notice: This module doesn't support parsing multipart format data, please use @koa/multer to parse multipart format data.

Install

NPM

$ npm i @koa/bodyparser --save

Usage

const Koa = require("koa");
const { bodyParser } = require("@koa/bodyparser");

const app = new Koa();
app.use(bodyParser());

app.use((ctx) => {
  // the parsed body will store in ctx.request.body
  // if nothing was parsed, body will be an empty object {}
  ctx.body = ctx.request.body;
});

Options

  • patchNode: patch request body to Node's ctx.req, default is false.
  • enableTypes: parser will only parse when request type hits enableTypes, support json/form/text/xml, default is ['json', 'form'].
  • encoding: requested encoding. Default is utf-8 by co-body.
  • formLimit: limit of the urlencoded body. If the body ends up being larger than this limit, a 413 error code is returned. Default is 56kb.
  • jsonLimit: limit of the json body. Default is 1mb.
  • textLimit: limit of the text body. Default is 1mb.
  • xmlLimit: limit of the xml body. Default is 1mb.
  • jsonStrict: when set to true, JSON parser will only accept arrays and objects. Default is true. See strict mode in co-body. In strict mode, ctx.request.body will always be an object(or array), this avoid lots of type judging. But text body will always return string type.
  • detectJSON: custom json request detect function. Default is null.

    app.use(
      bodyParser({
        detectJSON(ctx) {
          return /\.json$/i.test(ctx.path);
        },
      })
    );
  • extendTypes: support extend types:

    app.use(
      bodyParser({
        extendTypes: {
          // will parse application/x-javascript type body as a JSON string
          json: ["application/x-javascript"],
        },
      })
    );
  • onError: support custom error handle, if koa-bodyparser throw an error, you can customize the response like:

    app.use(
      bodyParser({
        onError(err, ctx) {
          ctx.throw(422, "body parse error");
        },
      })
    );
  • enableRawChecking: support the already parsed body on the raw request by override and prioritize the parsed value over the sended payload. (default is false)

  • parsedMethods: declares the HTTP methods where bodies will be parsed, default ['POST', 'PUT', 'PATCH'].

  • disableBodyParser: you can dynamic disable body parser by set ctx.disableBodyParser = true.

    app.use((ctx, next) => {
      if (ctx.path === "/disable") ctx.disableBodyParser = true;
      return next();
    });
    app.use(bodyParser());

Raw Body

You can access raw request body by ctx.request.rawBody after koa-bodyparser when:

  1. koa-bodyparser parsed the request body.
  2. ctx.request.rawBody is not present before koa-bodyparser.

Koa v1.x.x Support

To use koa-bodyparser with koa@1.x.x, please use bodyparser 2.x.

$ npm install koa-bodyparser@2 --save

usage

const Koa = require("koa");
const bodyParser = require("@koa/bodyparser");

const app = new Koa();
app.use(bodyParser());

app.use((ctx) => {
  // the parsed body will store in ctx.request.body
  // if nothing was parsed, body will be an empty object {}
  ctx.body = ctx.request.body;
});

Licences

MIT

changelog

5.1.2 / 2025-05-22

fixes

  • [031348a] - fix: add check for closed requests (#162) (zbrydon)

5.1.1 / 2024-04-09

fixes

  • [f27be45] - fix: Add koa as peerDeps and fix the encoding option propagation (#160) (3imed-jaberi)

5.1.0 / 2024-03-25

features

others

5.0.0 / 2023-06-25

features

4.4.1 / 2023-06-22

fixes

4.4.0 / 2023-03-15

features

fixes

others

4.3.0 / 2020-03-24

features

others

4.2.1 / 2018-05-21

others

4.2.0 / 2017-03-21

  • feat: ctx.request.rawBody to get raw request body (#70)

4.1.0 / 2017-03-02

  • deps: upgrade co-body@5 (#64)

4.0.0 / 2017-02-27

  • refactor: use async function and support koa@2 (#62)

2.3.0 / 2016-11-14

  • feat: support dynamic disable body parser

2.2.0 / 2016-05-16

  • feat: support enableTypes and text (#44)

2.1.0 / 2016-05-10

  • deps: co-body@4

2.0.1 / 2015-08-12

2.0.0 / 2015-05-07

  • deps: co-body@2, default to strict mode

1.6.0 / 2015-05-01

  • feat: support custom error handler

1.5.0 / 2015-04-04

  • Use an empty object instead of null, if no body is parsed

1.4.1 / 2015-03-10

1.4.0 / 2015-02-26

  • feat: custom json request detect

1.3.1 / 2015-01-27

  • fix: extend

1.3.0 / 2014-11-27

  • support extendTypes
  • Merge pull request #8 from coderhaoxin/json-patch
  • add support for json patch

1.2.0 / 2014-11-07

  • add example.js
  • bump dependencies
  • Merge pull request #7 from rudijs/develop
  • Add support for JSON-API

1.1.0 / 2014-10-28

1.0.0 / 2014-04-23

  • update readme
  • refactor

0.1.0 / 2014-03-06

  • Merge pull request #2 from fengmk2/remove-co
  • Remove co deps and improve coverage to 100%

0.0.2 / 2014-02-26

  • Merge pull request #1 from fengmk2/jsonLimit
  • add jsonLimit options to fix json and form body limit confuse

0.0.1 / 2014-02-18

  • update package name, merge middleware into module.exports
  • complete readme
  • complete bodyparser and bodyparser.middleware
  • Initial commit