Package Exports
- co-body
- co-body/index.js
- co-body/lib/form
- co-body/lib/form.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (co-body) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
co-body
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 totrue
, JSON parser will only accept arrays and objects; whenfalse
will accept anythingJSON.parse
accepts. Defaults totrue
. (alsostrict
mode will always return object).onProtoPoisoning
Defines what action theco-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 aSyntaxError
whenPrototype-Poisoning
happen.queryString
an object of options when parsing query strings and form data. See qs for more information.returnRawBody
when set totrue
, the return value ofco-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);