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

Package detail

koa-bodyparser

koajs2.7mMIT4.4.1TypeScript support: definitely-typed

a body parser for Koa

bodyParser, json, urlencoded, koa, body

readme

koa-bodyparser

NPM version build status Coveralls David deps node version

A body parser for koa, based on co-body. support json, form and text type body.

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

Install

NPM

Usage

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');

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

app.use(async 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

  • 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.
  • strict: 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: function (ctx) {
        return /\.json$/i.test(ctx.path);
      }
    }));
  • extendTypes: support extend types:

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

    app.use(bodyParser({
      onerror: function (err, ctx) {
        ctx.throw(422, 'body parse error');
      }
    }));
  • disableBodyParser: you can dynamic disable body parser by set ctx.disableBodyParser = true.

    app.use(async (ctx, next) => {
      if (ctx.path === '/disable') ctx.disableBodyParser = true;
      await 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 1 Support

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

npm install koa-bodyparser@2 --save

Licences


MIT

changelog

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