3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Build Status][travis-image]][travis-url]
6 [![Test Coverage][coveralls-image]][coveralls-url]
7 [![Gratipay][gratipay-image]][gratipay-url]
9 Node.js body parsing middleware.
11 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:
13 - [busboy](https://www.npmjs.org/package/busboy#readme) and [connect-busboy](https://www.npmjs.org/package/connect-busboy#readme)
14 - [multiparty](https://www.npmjs.org/package/multiparty#readme) and [connect-multiparty](https://www.npmjs.org/package/connect-multiparty#readme)
15 - [formidable](https://www.npmjs.org/package/formidable#readme)
16 - [multer](https://www.npmjs.org/package/multer#readme)
18 Other body parsers you might be interested in:
20 - [body](https://www.npmjs.org/package/body#readme)
21 - [co-body](https://www.npmjs.org/package/co-body#readme)
26 $ npm install body-parser
32 var bodyParser = require('body-parser')
35 ### bodyParser.json(options)
37 Returns middleware that only parses `json`. This parser accepts any Unicode encoding of the body and supports automatic inflation of `gzip` and `deflate` encodings.
41 - `strict` - only parse objects and arrays. (default: `true`)
42 - `inflate` - if deflated bodies will be inflated. (default: `true`)
43 - `limit` - maximum request body size. (default: `<100kb>`)
44 - `reviver` - passed to `JSON.parse()`
45 - `type` - request content-type to parse (default: `json`)
46 - `verify` - function to verify body content
48 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`).
50 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.
52 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).
54 ### bodyParser.raw(options)
56 Returns middleware that parses all bodies as a `Buffer`. This parser supports automatic inflation of `gzip` and `deflate` encodings.
60 - `inflate` - if deflated bodies will be inflated. (default: `true`)
61 - `limit` - maximum request body size. (default: `<100kb>`)
62 - `type` - request content-type to parse (default: `application/octet-stream`)
63 - `verify` - function to verify body content
65 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/*`).
67 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.
69 ### bodyParser.text(options)
71 Returns middleware that parses all bodies as a string. This parser supports automatic inflation of `gzip` and `deflate` encodings.
75 - `defaultCharset` - the default charset to parse as, if not specified in content-type. (default: `utf-8`)
76 - `inflate` - if deflated bodies will be inflated. (default: `true`)
77 - `limit` - maximum request body size. (default: `<100kb>`)
78 - `type` - request content-type to parse (default: `text/plain`)
79 - `verify` - function to verify body content
81 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/*`).
83 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.
85 ### bodyParser.urlencoded(options)
87 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.
91 - `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)
92 - `inflate` - if deflated bodies will be inflated. (default: `true`)
93 - `limit` - maximum request body size. (default: `<100kb>`)
94 - `parameterLimit` - maximum number of parameters. (default: `1000`)
95 - `type` - request content-type to parse (default: `urlencoded`)
96 - `verify` - function to verify body content
98 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).
100 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.
102 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`).
104 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.
108 A new `body` object containing the parsed data is populated on the `request` object after the middleware.
112 ### express/connect top-level generic
114 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.
117 var express = require('express')
118 var bodyParser = require('body-parser')
122 // parse application/x-www-form-urlencoded
123 app.use(bodyParser.urlencoded({ extended: false }))
125 // parse application/json
126 app.use(bodyParser.json())
128 app.use(function (req, res) {
129 res.setHeader('Content-Type', 'text/plain')
130 res.write('you posted:\n')
131 res.end(JSON.stringify(req.body, null, 2))
135 ### express route-specific
137 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.
140 var express = require('express')
141 var bodyParser = require('body-parser')
145 // create application/json parser
146 var jsonParser = bodyParser.json()
148 // create application/x-www-form-urlencoded parser
149 var urlencodedParser = bodyParser.urlencoded({ extended: false })
151 // POST /login gets urlencoded bodies
152 app.post('/login', urlencodedParser, function (req, res) {
153 if (!req.body) return res.sendStatus(400)
154 res.send('welcome, ' + req.body.username)
157 // POST /api/users gets JSON bodies
158 app.post('/api/users', jsonParser, function (req, res) {
159 if (!req.body) return res.sendStatus(400)
160 // create user in req.body
164 ### change content-type for parsers
166 All the parsers accept a `type` option which allows you to change the `Content-Type` that the middleware will parse.
169 // parse various different custom JSON types as JSON
170 app.use(bodyParser.json({ type: 'application/*+json' }))
172 // parse some custom thing into a Buffer
173 app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
175 // parse an HTML body into a string
176 app.use(bodyParser.text({ type: 'text/html' }))
183 [npm-image]: https://img.shields.io/npm/v/body-parser.svg?style=flat
184 [npm-url]: https://npmjs.org/package/body-parser
185 [travis-image]: https://img.shields.io/travis/expressjs/body-parser.svg?style=flat
186 [travis-url]: https://travis-ci.org/expressjs/body-parser
187 [coveralls-image]: https://img.shields.io/coveralls/expressjs/body-parser.svg?style=flat
188 [coveralls-url]: https://coveralls.io/r/expressjs/body-parser?branch=master
189 [downloads-image]: https://img.shields.io/npm/dm/body-parser.svg?style=flat
190 [downloads-url]: https://npmjs.org/package/body-parser
191 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat
192 [gratipay-url]: https://www.gratipay.com/dougwilson/