body-parser
Advanced tools
Comparing version 1.11.0 to 1.12.0
@@ -0,1 +1,17 @@ | ||
1.12.0 / 2015-02-13 | ||
=================== | ||
* add `debug` messages | ||
* accept a function for the `type` option | ||
* use `content-type` to parse `Content-Type` headers | ||
* deps: iconv-lite@0.4.7 | ||
- Gracefully support enumerables on `Object.prototype` | ||
* deps: raw-body@1.3.3 | ||
- deps: iconv-lite@0.4.7 | ||
* deps: type-is@~1.6.0 | ||
- fix argument reassignment | ||
- fix false-positives in `hasBody` `Transfer-Encoding` check | ||
- support wildcard for both type and subtype (`*/*`) | ||
- deps: mime-types@~2.0.9 | ||
1.11.0 / 2015-01-30 | ||
@@ -2,0 +18,0 @@ =================== |
/*! | ||
* body-parser | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* Copyright(c) 2014-2015 Douglas Christopher Wilson | ||
* MIT Licensed | ||
@@ -14,3 +14,2 @@ */ | ||
var onFinished = require('on-finished') | ||
var typer = require('media-typer') | ||
var zlib = require('zlib') | ||
@@ -31,7 +30,8 @@ | ||
* @param {function} parse | ||
* @param {object} options | ||
* @param {function} debug | ||
* @param {object} [options] | ||
* @api private | ||
*/ | ||
function read(req, res, next, parse, options) { | ||
function read(req, res, next, parse, debug, options) { | ||
var length | ||
@@ -43,4 +43,6 @@ var stream | ||
var opts = options || {} | ||
try { | ||
stream = contentstream(req, options.inflate) | ||
stream = contentstream(req, debug, opts.inflate) | ||
length = stream.length | ||
@@ -52,11 +54,10 @@ delete stream.length | ||
options = options || {} | ||
options.length = length | ||
opts.length = length | ||
var encoding = options.encoding !== null | ||
? options.encoding || 'utf-8' | ||
var encoding = opts.encoding !== null | ||
? opts.encoding || 'utf-8' | ||
: null | ||
var verify = options.verify | ||
var verify = opts.verify | ||
options.encoding = verify | ||
opts.encoding = verify | ||
? null | ||
@@ -66,3 +67,4 @@ : encoding | ||
// read body | ||
getBody(stream, options, function (err, body) { | ||
debug('read body') | ||
getBody(stream, opts, function (err, body) { | ||
if (err) { | ||
@@ -91,2 +93,3 @@ if (!err.status) { | ||
try { | ||
debug('verify body') | ||
verify(req, res, body, encoding) | ||
@@ -101,2 +104,3 @@ } catch (err) { | ||
try { | ||
debug('parse body') | ||
body = typeof body !== 'string' && encoding !== null | ||
@@ -122,2 +126,3 @@ ? iconv.decode(body, encoding) | ||
* @param {object} req | ||
* @param {function} debug | ||
* @param {boolean} [inflate=true] | ||
@@ -128,3 +133,3 @@ * @return {object} | ||
function contentstream(req, inflate) { | ||
function contentstream(req, debug, inflate) { | ||
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase() | ||
@@ -135,2 +140,4 @@ var err | ||
debug('content-encoding "%s"', encoding) | ||
if (inflate === false && encoding !== 'identity') { | ||
@@ -145,2 +152,3 @@ err = new Error('content encoding unsupported') | ||
stream = zlib.createInflate() | ||
debug('inflate body') | ||
req.pipe(stream) | ||
@@ -150,2 +158,3 @@ break | ||
stream = zlib.createGunzip() | ||
debug('gunzip body') | ||
req.pipe(stream) | ||
@@ -152,0 +161,0 @@ break |
/*! | ||
* body-parser | ||
* Copyright(c) 2014 Jonathan Ong | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* Copyright(c) 2014-2015 Douglas Christopher Wilson | ||
* MIT Licensed | ||
@@ -13,4 +13,5 @@ */ | ||
var bytes = require('bytes') | ||
var contentType = require('content-type') | ||
var debug = require('debug')('body-parser:json') | ||
var read = require('../read') | ||
var typer = require('media-typer') | ||
var typeis = require('type-is') | ||
@@ -62,2 +63,7 @@ | ||
// create the appropriate type checking function | ||
var shouldParse = typeof type !== 'function' | ||
? typeChecker(type) | ||
: type | ||
function parse(body) { | ||
@@ -74,2 +80,3 @@ if (body.length === 0) { | ||
if (first !== '{' && first !== '[') { | ||
debug('strict violation') | ||
throw new Error('invalid json') | ||
@@ -79,2 +86,3 @@ } | ||
debug('parse json') | ||
return JSON.parse(body, reviver) | ||
@@ -84,9 +92,22 @@ } | ||
return function jsonParser(req, res, next) { | ||
if (req._body) return next() | ||
if (req._body) { | ||
return debug('body already parsed'), next() | ||
} | ||
req.body = req.body || {} | ||
if (!typeis(req, type)) return next() | ||
// skip requests without bodies | ||
if (!typeis.hasBody(req)) { | ||
return debug('skip empty body'), next() | ||
} | ||
// RFC 7159 sec 8.1 | ||
var charset = (typer.parse(req).parameters.charset || 'utf-8').toLowerCase() | ||
debug('content-type %s', JSON.stringify(req.headers['content-type'])) | ||
// determine if request should be parsed | ||
if (!shouldParse(req)) { | ||
return debug('skip parsing'), next() | ||
} | ||
// assert charset per RFC 7159 sec 8.1 | ||
var charset = getCharset(req) || 'utf-8' | ||
if (charset.substr(0, 4) !== 'utf-') { | ||
@@ -96,8 +117,7 @@ var err = new Error('unsupported charset "' + charset.toUpperCase() + '"') | ||
err.status = 415 | ||
next(err) | ||
return | ||
return debug('invalid charset'), next(err) | ||
} | ||
// read | ||
read(req, res, next, parse, { | ||
read(req, res, next, parse, debug, { | ||
encoding: charset, | ||
@@ -124,1 +144,29 @@ inflate: inflate, | ||
} | ||
/** | ||
* Get the charset of a request. | ||
* | ||
* @param {object} req | ||
* @api private | ||
*/ | ||
function getCharset(req) { | ||
try { | ||
return contentType.parse(req).parameters.charset.toLowerCase() | ||
} catch (e) { | ||
return undefined | ||
} | ||
} | ||
/** | ||
* Get the simple type checker. | ||
* | ||
* @param {string} type | ||
* @return {function} | ||
*/ | ||
function typeChecker(type) { | ||
return function checkType(req) { | ||
return Boolean(typeis(req, type)) | ||
} | ||
} |
/*! | ||
* body-parser | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* Copyright(c) 2014-2015 Douglas Christopher Wilson | ||
* MIT Licensed | ||
@@ -12,2 +12,3 @@ */ | ||
var bytes = require('bytes') | ||
var debug = require('debug')('body-parser:raw') | ||
var read = require('../read') | ||
@@ -44,2 +45,7 @@ var typeis = require('type-is') | ||
// create the appropriate type checking function | ||
var shouldParse = typeof type !== 'function' | ||
? typeChecker(type) | ||
: type | ||
function parse(buf) { | ||
@@ -50,9 +56,22 @@ return buf | ||
return function rawParser(req, res, next) { | ||
if (req._body) return next() | ||
if (req._body) { | ||
return debug('body already parsed'), next() | ||
} | ||
req.body = req.body || {} | ||
if (!typeis(req, type)) return next() | ||
// skip requests without bodies | ||
if (!typeis.hasBody(req)) { | ||
return debug('skip empty body'), next() | ||
} | ||
debug('content-type %s', JSON.stringify(req.headers['content-type'])) | ||
// determine if request should be parsed | ||
if (!shouldParse(req)) { | ||
return debug('skip parsing'), next() | ||
} | ||
// read | ||
read(req, res, next, parse, { | ||
read(req, res, next, parse, debug, { | ||
encoding: null, | ||
@@ -65,1 +84,14 @@ inflate: inflate, | ||
} | ||
/** | ||
* Get the simple type checker. | ||
* | ||
* @param {string} type | ||
* @return {function} | ||
*/ | ||
function typeChecker(type) { | ||
return function checkType(req) { | ||
return Boolean(typeis(req, type)) | ||
} | ||
} |
/*! | ||
* body-parser | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* Copyright(c) 2014-2015 Douglas Christopher Wilson | ||
* MIT Licensed | ||
@@ -12,5 +12,6 @@ */ | ||
var bytes = require('bytes') | ||
var contentType = require('content-type') | ||
var debug = require('debug')('body-parser:text') | ||
var read = require('../read') | ||
var typeis = require('type-is') | ||
var typer = require('media-typer') | ||
@@ -46,2 +47,7 @@ /** | ||
// create the appropriate type checking function | ||
var shouldParse = typeof type !== 'function' | ||
? typeChecker(type) | ||
: type | ||
function parse(buf) { | ||
@@ -52,12 +58,25 @@ return buf | ||
return function textParser(req, res, next) { | ||
if (req._body) return next() | ||
if (req._body) { | ||
return debug('body already parsed'), next() | ||
} | ||
req.body = req.body || {} | ||
if (!typeis(req, type)) return next() | ||
// skip requests without bodies | ||
if (!typeis.hasBody(req)) { | ||
return debug('skip empty body'), next() | ||
} | ||
debug('content-type %s', JSON.stringify(req.headers['content-type'])) | ||
// determine if request should be parsed | ||
if (!shouldParse(req)) { | ||
return debug('skip parsing'), next() | ||
} | ||
// get charset | ||
var charset = typer.parse(req).parameters.charset || defaultCharset | ||
var charset = getCharset(req) || defaultCharset | ||
// read | ||
read(req, res, next, parse, { | ||
read(req, res, next, parse, debug, { | ||
encoding: charset, | ||
@@ -70,1 +89,29 @@ inflate: inflate, | ||
} | ||
/** | ||
* Get the charset of a request. | ||
* | ||
* @param {object} req | ||
* @api private | ||
*/ | ||
function getCharset(req) { | ||
try { | ||
return contentType.parse(req).parameters.charset.toLowerCase() | ||
} catch (e) { | ||
return undefined | ||
} | ||
} | ||
/** | ||
* Get the simple type checker. | ||
* | ||
* @param {string} type | ||
* @return {function} | ||
*/ | ||
function typeChecker(type) { | ||
return function checkType(req) { | ||
return Boolean(typeis(req, type)) | ||
} | ||
} |
/*! | ||
* body-parser | ||
* Copyright(c) 2014 Jonathan Ong | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* Copyright(c) 2014-2015 Douglas Christopher Wilson | ||
* MIT Licensed | ||
@@ -13,5 +13,6 @@ */ | ||
var bytes = require('bytes') | ||
var contentType = require('content-type') | ||
var debug = require('debug')('body-parser:urlencoded') | ||
var deprecate = require('depd')('body-parser') | ||
var read = require('../read') | ||
var typer = require('media-typer') | ||
var typeis = require('type-is') | ||
@@ -59,2 +60,3 @@ | ||
// create the appropriate query parser | ||
var queryparse = extended | ||
@@ -64,2 +66,7 @@ ? extendedparser(options) | ||
// create the appropriate type checking function | ||
var shouldParse = typeof type !== 'function' | ||
? typeChecker(type) | ||
: type | ||
function parse(body) { | ||
@@ -72,8 +79,22 @@ return body.length | ||
return function urlencodedParser(req, res, next) { | ||
if (req._body) return next(); | ||
if (req._body) { | ||
return debug('body already parsed'), next() | ||
} | ||
req.body = req.body || {} | ||
if (!typeis(req, type)) return next(); | ||
// skip requests without bodies | ||
if (!typeis.hasBody(req)) { | ||
return debug('skip empty body'), next() | ||
} | ||
var charset = (typer.parse(req).parameters.charset || 'utf-8').toLowerCase() | ||
debug('content-type %s', JSON.stringify(req.headers['content-type'])) | ||
// determine if request should be parsed | ||
if (!shouldParse(req)) { | ||
return debug('skip parsing'), next() | ||
} | ||
// assert charset | ||
var charset = getCharset(req) || 'utf-8' | ||
if (charset !== 'utf-8') { | ||
@@ -83,8 +104,8 @@ var err = new Error('unsupported charset "' + charset.toUpperCase() + '"') | ||
err.status = 415 | ||
next(err) | ||
return | ||
return debug('invalid charset'), next(err) | ||
} | ||
// read | ||
read(req, res, next, parse, { | ||
read(req, res, next, parse, debug, { | ||
debug: debug, | ||
encoding: charset, | ||
@@ -124,2 +145,3 @@ inflate: inflate, | ||
err.status = 413 | ||
debug('too many parameters') | ||
throw err | ||
@@ -130,2 +152,3 @@ } | ||
debug('parse extended urlencoding') | ||
return parse(body, { | ||
@@ -140,2 +163,17 @@ arrayLimit: arrayLimit, | ||
/** | ||
* Get the charset of a request. | ||
* | ||
* @param {object} req | ||
* @api private | ||
*/ | ||
function getCharset(req) { | ||
try { | ||
return contentType.parse(req).parameters.charset.toLowerCase() | ||
} catch (e) { | ||
return undefined | ||
} | ||
} | ||
/** | ||
* Count the number of parameters, stopping once limit reached | ||
@@ -211,7 +249,22 @@ * | ||
err.status = 413 | ||
debug('too many parameters') | ||
throw err | ||
} | ||
debug('parse urlencoding') | ||
return parse(body, undefined, undefined, {maxKeys: parameterLimit}) | ||
} | ||
} | ||
/** | ||
* Get the simple type checker. | ||
* | ||
* @param {string} type | ||
* @return {function} | ||
*/ | ||
function typeChecker(type) { | ||
return function checkType(req) { | ||
return Boolean(typeis(req, type)) | ||
} | ||
} |
{ | ||
"name": "body-parser", | ||
"description": "Node.js body parsing middleware", | ||
"version": "1.11.0", | ||
"version": "1.12.0", | ||
"contributors": [ | ||
@@ -13,9 +13,10 @@ "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"bytes": "1.0.0", | ||
"content-type": "~1.0.1", | ||
"debug": "~2.1.1", | ||
"depd": "~1.0.0", | ||
"iconv-lite": "0.4.6", | ||
"media-typer": "0.3.0", | ||
"iconv-lite": "0.4.7", | ||
"on-finished": "~2.2.0", | ||
"qs": "2.3.3", | ||
"raw-body": "1.3.2", | ||
"type-is": "~1.5.6" | ||
"raw-body": "1.3.3", | ||
"type-is": "~1.6.0" | ||
}, | ||
@@ -22,0 +23,0 @@ "devDependencies": { |
258
README.md
@@ -11,9 +11,20 @@ # body-parser | ||
This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules: | ||
_This does not handle multipart bodies_, due to their complex and typically | ||
large nature. For multipart bodies, you may be interested in the following | ||
modules: | ||
- [busboy](https://www.npmjs.org/package/busboy#readme) and [connect-busboy](https://www.npmjs.org/package/connect-busboy#readme) | ||
- [multiparty](https://www.npmjs.org/package/multiparty#readme) and [connect-multiparty](https://www.npmjs.org/package/connect-multiparty#readme) | ||
- [formidable](https://www.npmjs.org/package/formidable#readme) | ||
- [multer](https://www.npmjs.org/package/multer#readme) | ||
* [busboy](https://www.npmjs.org/package/busboy#readme) and | ||
[connect-busboy](https://www.npmjs.org/package/connect-busboy#readme) | ||
* [multiparty](https://www.npmjs.org/package/multiparty#readme) and | ||
[connect-multiparty](https://www.npmjs.org/package/connect-multiparty#readme) | ||
* [formidable](https://www.npmjs.org/package/formidable#readme) | ||
* [multer](https://www.npmjs.org/package/multer#readme) | ||
This module provides the following parsers: | ||
* [JSON body parser](#bodyparserjsonoptions) | ||
* [Raw body parser](#bodyparserrawoptions) | ||
* [Text body parser](#bodyparsertextoptions) | ||
* [URL-encoded form body parser](#bodyparserurlencodedoptions) | ||
Other body parsers you might be interested in: | ||
@@ -38,75 +49,207 @@ | ||
Returns middleware that only parses `json`. This parser accepts any Unicode encoding of the body and supports automatic inflation of `gzip` and `deflate` encodings. | ||
Returns middleware that only parses `json`. This parser accepts any Unicode | ||
encoding of the body and supports automatic inflation of `gzip` and `deflate` | ||
encodings. | ||
The options are: | ||
A new `body` object containing the parsed data is populated on the `request` | ||
object after the middleware (i.e. `req.body`). | ||
- `strict` - only parse objects and arrays. (default: `true`) | ||
- `inflate` - if deflated bodies will be inflated. (default: `true`) | ||
- `limit` - maximum request body size. (default: `<100kb>`) | ||
- `reviver` - passed to `JSON.parse()` | ||
- `type` - request content-type to parse (default: `json`) | ||
- `verify` - function to verify body content | ||
#### Options | ||
The `type` argument is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library. This can be an extension name (like `json`), a mime type (like `application/json`), or a mime time with a wildcard (like `*/json`). | ||
The `json` function takes an option `options` object that may contain any of | ||
the following keys: | ||
The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error. | ||
##### inflate | ||
The `reviver` argument is passed directly to `JSON.parse` as the second argument. You can find more information on this argument [in the MDN documentation about JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter). | ||
When set to `true`, then deflated (compressed) bodies will be inflated; when | ||
`false`, deflated bodies are rejected. Defaults to `true`. | ||
##### limit | ||
Controls the maximum request body size. If this is a number, then the value | ||
specifies the number of bytes; if it is a string, the value is passed to the | ||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults | ||
to `'100kb'`. | ||
##### reviver | ||
The `reviver` option is passed directly to `JSON.parse` as the second | ||
argument. You can find more information on this argument | ||
[in the MDN documentation about JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter). | ||
##### strict | ||
When set to `true`, will only accept arrays and objects; when `false` will | ||
accept anything `JSON.parse` accepts. Defaults to `true`. | ||
##### type | ||
The `type` option is used to determine what media type the middleware will | ||
parse. This option can be a function or a string. If a string, `type` option | ||
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) | ||
library and this can be an extension name (like `json`), a mime type (like | ||
`application/json`), or a mime time with a wildcard (like `*/*` or `*/json`). | ||
If a function, the `type` option is called as `fn(req)` and the request is | ||
parsed if it returns a truthy value. Defaults to `json`. | ||
##### verify | ||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, | ||
where `buf` is a `Buffer` of the raw request body and `encoding` is the | ||
encoding of the request. The parsing can be aborted by throwing an error. | ||
### bodyParser.raw(options) | ||
Returns middleware that parses all bodies as a `Buffer`. This parser supports automatic inflation of `gzip` and `deflate` encodings. | ||
Returns middleware that parses all bodies as a `Buffer`. This parser | ||
supports automatic inflation of `gzip` and `deflate` encodings. | ||
The options are: | ||
A new `body` object containing the parsed data is populated on the `request` | ||
object after the middleware (i.e. `req.body`). This will be a `Buffer` object | ||
of the body. | ||
- `inflate` - if deflated bodies will be inflated. (default: `true`) | ||
- `limit` - maximum request body size. (default: `<100kb>`) | ||
- `type` - request content-type to parse (default: `application/octet-stream`) | ||
- `verify` - function to verify body content | ||
#### Options | ||
The `type` argument is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library. This can be an extension name (like `bin`), a mime type (like `application/octet-stream`), or a mime time with a wildcard (like `application/*`). | ||
The `raw` function takes an option `options` object that may contain any of | ||
the following keys: | ||
The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error. | ||
##### inflate | ||
When set to `true`, then deflated (compressed) bodies will be inflated; when | ||
`false`, deflated bodies are rejected. Defaults to `true`. | ||
##### limit | ||
Controls the maximum request body size. If this is a number, then the value | ||
specifies the number of bytes; if it is a string, the value is passed to the | ||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults | ||
to `'100kb'`. | ||
##### type | ||
The `type` option is used to determine what media type the middleware will | ||
parse. This option can be a function or a string. If a string, `type` option | ||
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) | ||
library and this can be an extension name (like `bin`), a mime type (like | ||
`application/octet-stream`), or a mime time with a wildcard (like `*/*` or | ||
`application/*`). If a function, the `type` option is called as `fn(req)` | ||
and the request is parsed if it returns a truthy value. Defaults to | ||
`application/octet-stream`. | ||
##### verify | ||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, | ||
where `buf` is a `Buffer` of the raw request body and `encoding` is the | ||
encoding of the request. The parsing can be aborted by throwing an error. | ||
### bodyParser.text(options) | ||
Returns middleware that parses all bodies as a string. This parser supports automatic inflation of `gzip` and `deflate` encodings. | ||
Returns middleware that parses all bodies as a string. This parser supports | ||
automatic inflation of `gzip` and `deflate` encodings. | ||
The options are: | ||
A new `body` string containing the parsed data is populated on the `request` | ||
object after the middleware (i.e. `req.body`). This will be a string of the | ||
body. | ||
- `defaultCharset` - the default charset to parse as, if not specified in content-type. (default: `utf-8`) | ||
- `inflate` - if deflated bodies will be inflated. (default: `true`) | ||
- `limit` - maximum request body size. (default: `<100kb>`) | ||
- `type` - request content-type to parse (default: `text/plain`) | ||
- `verify` - function to verify body content | ||
#### Options | ||
The `type` argument is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library. This can be an extension name (like `txt`), a mime type (like `text/plain`), or a mime time with a wildcard (like `text/*`). | ||
The `text` function takes an option `options` object that may contain any of | ||
the following keys: | ||
The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error. | ||
##### defaultCharset | ||
Specify the default character set for the text content if the charset is not | ||
specified in the `Content-Type` header of the request. Defaults to `utf-8`. | ||
##### inflate | ||
When set to `true`, then deflated (compressed) bodies will be inflated; when | ||
`false`, deflated bodies are rejected. Defaults to `true`. | ||
##### limit | ||
Controls the maximum request body size. If this is a number, then the value | ||
specifies the number of bytes; if it is a string, the value is passed to the | ||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults | ||
to `'100kb'`. | ||
##### type | ||
The `type` option is used to determine what media type the middleware will | ||
parse. This option can be a function or a string. If a string, `type` option | ||
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) | ||
library and this can be an extension name (like `txt`), a mime type (like | ||
`text/plain`), or a mime time with a wildcard (like `*/*` or `text/*`). | ||
If a function, the `type` option is called as `fn(req)` and the request is | ||
parsed if it returns a truthy value. Defaults to `text/plain`. | ||
##### verify | ||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, | ||
where `buf` is a `Buffer` of the raw request body and `encoding` is the | ||
encoding of the request. The parsing can be aborted by throwing an error. | ||
### bodyParser.urlencoded(options) | ||
Returns middleware that only parses `urlencoded` bodies. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of `gzip` and `deflate` encodings. | ||
Returns middleware that only parses `urlencoded` bodies. This parser accepts | ||
only UTF-8 encoding of the body and supports automatic inflation of `gzip` | ||
and `deflate` encodings. | ||
The options are: | ||
A new `body` object containing the parsed data is populated on the `request` | ||
object after the middleware (i.e. `req.body`). This object will contain | ||
key-value pairs, where the value can be a string or array (when `extended` is | ||
`false`), or any type (when `extended` is `true`). | ||
- `extended` - parse extended syntax with the [qs](https://www.npmjs.org/package/qs#readme) module. (default: `true`, but using the default has been deprecated. Please research into the difference between `qs` and `querystring` and choose the appropriate setting) | ||
- `inflate` - if deflated bodies will be inflated. (default: `true`) | ||
- `limit` - maximum request body size. (default: `<100kb>`) | ||
- `parameterLimit` - maximum number of parameters. (default: `1000`) | ||
- `type` - request content-type to parse (default: `urlencoded`) | ||
- `verify` - function to verify body content | ||
#### Options | ||
The `extended` argument allows to choose between parsing the urlencoded data with the `querystring` library (when `false`) or the `qs` library (when `true`). The "extended" syntax allows for rich objects and arrays to be encoded into the urlencoded format, allowing for a JSON-like experience with urlencoded. For more information, please [see the qs library](https://www.npmjs.org/package/qs#readme). | ||
The `urlencoded` function takes an option `options` object that may contain | ||
any of the following keys: | ||
The `parameterLimit` argument controls the maximum number of parameters that are allowed in the urlencoded data. If a request contains more parameters than this value, a 413 will be returned to the client. | ||
##### extended | ||
The `type` argument is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library. This can be an extension name (like `urlencoded`), a mime type (like `application/x-www-form-urlencoded`), or a mime time with a wildcard (like `*/x-www-form-urlencoded`). | ||
The `extended` option allows to choose between parsing the URL-encoded data | ||
with the `querystring` library (when `false`) or the `qs` library (when | ||
`true`). The "extended" syntax allows for rich objects and arrays to be | ||
encoded into the URL-encoded format, allowing for a JSON-like experience | ||
with URL-encoded. For more information, please | ||
[see the qs library](https://www.npmjs.org/package/qs#readme). | ||
The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error. | ||
Defaults to `true`, but using the default has been deprecated. Please | ||
research into the difference between `qs` and `querystring` and choose the | ||
appropriate setting. | ||
### req.body | ||
##### inflate | ||
A new `body` object containing the parsed data is populated on the `request` object after the middleware. | ||
When set to `true`, then deflated (compressed) bodies will be inflated; when | ||
`false`, deflated bodies are rejected. Defaults to `true`. | ||
##### limit | ||
Controls the maximum request body size. If this is a number, then the value | ||
specifies the number of bytes; if it is a string, the value is passed to the | ||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults | ||
to `'100kb'`. | ||
##### parameterLimit | ||
The `parameterLimit` option controls the maximum number of parameters that | ||
are allowed in the URL-encoded data. If a request contains more parameters | ||
than this value, a 413 will be returned to the client. Defaults to `1000`. | ||
##### type | ||
The `type` option is used to determine what media type the middleware will | ||
parse. This option can be a function or a string. If a string, `type` option | ||
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) | ||
library and this can be an extension name (like `urlencoded`), a mime type (like | ||
`application/x-www-form-urlencoded`), or a mime time with a wildcard (like | ||
`*/x-www-form-urlencoded`). If a function, the `type` option is called as | ||
`fn(req)` and the request is parsed if it returns a truthy value. Defaults | ||
to `urlencoded`. | ||
##### verify | ||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, | ||
where `buf` is a `Buffer` of the raw request body and `encoding` is the | ||
encoding of the request. The parsing can be aborted by throwing an error. | ||
## Examples | ||
@@ -116,3 +259,5 @@ | ||
This example demonstrates adding a generic JSON and urlencoded parser as a top-level middleware, which will parse the bodies of all incoming requests. This is the simplest setup. | ||
This example demonstrates adding a generic JSON and URL-encoded parser as a | ||
top-level middleware, which will parse the bodies of all incoming requests. | ||
This is the simplest setup. | ||
@@ -140,3 +285,5 @@ ```js | ||
This example demonstrates adding body parsers specifically to the routes that need them. In general, this is the most recommend way to use body-parser with express. | ||
This example demonstrates adding body parsers specifically to the routes that | ||
need them. In general, this is the most recommend way to use body-parser with | ||
express. | ||
@@ -170,3 +317,4 @@ ```js | ||
All the parsers accept a `type` option which allows you to change the `Content-Type` that the middleware will parse. | ||
All the parsers accept a `type` option which allows you to change the | ||
`Content-Type` that the middleware will parse. | ||
@@ -188,11 +336,11 @@ ```js | ||
[npm-image]: https://img.shields.io/npm/v/body-parser.svg?style=flat | ||
[npm-image]: https://img.shields.io/npm/v/body-parser.svg | ||
[npm-url]: https://npmjs.org/package/body-parser | ||
[travis-image]: https://img.shields.io/travis/expressjs/body-parser.svg?style=flat | ||
[travis-image]: https://img.shields.io/travis/expressjs/body-parser/master.svg | ||
[travis-url]: https://travis-ci.org/expressjs/body-parser | ||
[coveralls-image]: https://img.shields.io/coveralls/expressjs/body-parser.svg?style=flat | ||
[coveralls-image]: https://img.shields.io/coveralls/expressjs/body-parser/master.svg | ||
[coveralls-url]: https://coveralls.io/r/expressjs/body-parser?branch=master | ||
[downloads-image]: https://img.shields.io/npm/dm/body-parser.svg?style=flat | ||
[downloads-image]: https://img.shields.io/npm/dm/body-parser.svg | ||
[downloads-url]: https://npmjs.org/package/body-parser | ||
[gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat | ||
[gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg | ||
[gratipay-url]: https://www.gratipay.com/dougwilson/ |
Sorry, the diff of this file is not supported yet
38896
727
341
9
+ Addedcontent-type@~1.0.1
+ Addeddebug@~2.1.1
+ Addedcontent-type@1.0.5(transitive)
+ Addeddebug@2.1.3(transitive)
+ Addediconv-lite@0.4.7(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@0.7.0(transitive)
+ Addedraw-body@1.3.3(transitive)
+ Addedtype-is@1.6.18(transitive)
- Removedmedia-typer@0.3.0
- Removediconv-lite@0.4.6(transitive)
- Removedmime-db@1.12.0(transitive)
- Removedmime-types@2.0.14(transitive)
- Removedraw-body@1.3.2(transitive)
- Removedtype-is@1.5.7(transitive)
Updatediconv-lite@0.4.7
Updatedraw-body@1.3.3
Updatedtype-is@~1.6.0