co-body

request body parsing for co

MIT 21 个版本
安装
npm install co-body
yarn add co-body
pnpm add co-body
bun add co-body
README

co-body

NPM version CI Test coverage npm download

Parse request bodies with generators inspired by Raynos/body.

Installation

$ npm install co-body

Options

  • limit number or string representing the request size limit (1mb for json and 56kb for form-urlencoded)
  • strict when set to true, JSON parser will only accept arrays and objects; when false will accept anything JSON.parse accepts. Defaults to true. (also strict mode will always return object).
  • onProtoPoisoning Defines what action the co-body lib must take when parsing a JSON object with __proto__. This functionality is provided by bourne. See Prototype-Poisoning for more details about prototype poisoning attacks. Possible values are 'error', 'remove' and 'ignore'. Default to 'error', it will throw a SyntaxError when Prototype-Poisoning happen.
  • queryString an object of options when parsing query strings and form data. See qs for more information.
  • returnRawBody when set to true, the return value of co-body will be an object with two properties: { parsed: /* parsed value */, raw: /* raw body */}.
  • jsonTypes is used to determine what media type co-body will parse as json, this option is passed directly to the type-is library.
  • formTypes is used to determine what media type co-body will parse as form, this option is passed directly to the type-is library.
  • textTypes is used to determine what media type co-body will parse as text, this option is passed directly to the type-is library.

more options available via raw-body:

Example

// application/json
var body = await parse.json(req);

// explicit limit
var body = await parse.json(req, { limit: '10kb' });

// application/x-www-form-urlencoded
var body = await parse.form(req);

// text/plain
var body = await parse.text(req);

// either
var body = await parse(req);

// custom type
var body = await parse(req, { textTypes: ['text', 'html'] });

Koa

This lib also supports ctx.req in Koa (or other libraries), so that you may simply use this instead of this.req.

// application/json
var body = await parse.json(this);

// application/x-www-form-urlencoded
var body = await parse.form(this);

// text/plain
var body = await parse.text(this);

// either
var body = await parse(this);

License

MIT

版本列表
6.2.0 2024-06-05
6.1.0 2020-10-08
6.0.0 2018-05-21
5.2.0 2018-05-02
5.1.1 2017-03-24
5.1.0 2017-03-21
5.0.3 2017-03-19
5.0.2 2017-03-10
5.0.1 2017-03-06
5.0.0 2017-03-01
4.2.0 2016-05-04
4.1.0 2016-05-04
4.0.0 2015-08-14
3.1.0 2015-08-05
3.0.0 2015-07-25
2.0.0 2015-05-07
1.2.0 2015-04-28
1.1.0 2015-02-27
1.0.0 2014-08-06
0.0.2 2014-06-10
0.0.1 2013-11-28