| 1 | # body-parser
|
|---|
| 2 |
|
|---|
| 3 | [![NPM Version][npm-version-image]][npm-url]
|
|---|
| 4 | [![NPM Downloads][npm-downloads-image]][npm-url]
|
|---|
| 5 | [![Build Status][ci-image]][ci-url]
|
|---|
| 6 | [![Test Coverage][coveralls-image]][coveralls-url]
|
|---|
| 7 | [![OpenSSF Scorecard Badge][ossf-scorecard-badge]][ossf-scorecard-visualizer]
|
|---|
| 8 |
|
|---|
| 9 | Node.js body parsing middleware.
|
|---|
| 10 |
|
|---|
| 11 | Parse incoming request bodies in a middleware before your handlers, available
|
|---|
| 12 | under the `req.body` property.
|
|---|
| 13 |
|
|---|
| 14 | **Note** As `req.body`'s shape is based on user-controlled input, all
|
|---|
| 15 | properties and values in this object are untrusted and should be validated
|
|---|
| 16 | before trusting. For example, `req.body.foo.toString()` may fail in multiple
|
|---|
| 17 | ways, for example the `foo` property may not be there or may not be a string,
|
|---|
| 18 | and `toString` may not be a function and instead a string or other user input.
|
|---|
| 19 |
|
|---|
| 20 | [Learn about the anatomy of an HTTP transaction in Node.js](https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/).
|
|---|
| 21 |
|
|---|
| 22 | _This does not handle multipart bodies_, due to their complex and typically
|
|---|
| 23 | large nature. For multipart bodies, you may be interested in the following
|
|---|
| 24 | modules:
|
|---|
| 25 |
|
|---|
| 26 | * [busboy](https://www.npmjs.org/package/busboy#readme) and
|
|---|
| 27 | [connect-busboy](https://www.npmjs.org/package/connect-busboy#readme)
|
|---|
| 28 | * [multiparty](https://www.npmjs.org/package/multiparty#readme) and
|
|---|
| 29 | [connect-multiparty](https://www.npmjs.org/package/connect-multiparty#readme)
|
|---|
| 30 | * [formidable](https://www.npmjs.org/package/formidable#readme)
|
|---|
| 31 | * [multer](https://www.npmjs.org/package/multer#readme)
|
|---|
| 32 |
|
|---|
| 33 | This module provides the following parsers:
|
|---|
| 34 |
|
|---|
| 35 | * [JSON body parser](#bodyparserjsonoptions)
|
|---|
| 36 | * [Raw body parser](#bodyparserrawoptions)
|
|---|
| 37 | * [Text body parser](#bodyparsertextoptions)
|
|---|
| 38 | * [URL-encoded form body parser](#bodyparserurlencodedoptions)
|
|---|
| 39 |
|
|---|
| 40 | Other body parsers you might be interested in:
|
|---|
| 41 |
|
|---|
| 42 | - [body](https://www.npmjs.org/package/body#readme)
|
|---|
| 43 | - [co-body](https://www.npmjs.org/package/co-body#readme)
|
|---|
| 44 |
|
|---|
| 45 | ## Installation
|
|---|
| 46 |
|
|---|
| 47 | ```sh
|
|---|
| 48 | $ npm install body-parser
|
|---|
| 49 | ```
|
|---|
| 50 |
|
|---|
| 51 | ## API
|
|---|
| 52 |
|
|---|
| 53 | ```js
|
|---|
| 54 | var bodyParser = require('body-parser')
|
|---|
| 55 | ```
|
|---|
| 56 |
|
|---|
| 57 | The `bodyParser` object exposes various factories to create middlewares. All
|
|---|
| 58 | middlewares will populate the `req.body` property with the parsed body when
|
|---|
| 59 | the `Content-Type` request header matches the `type` option, or an empty
|
|---|
| 60 | object (`{}`) if there was no body to parse, the `Content-Type` was not matched,
|
|---|
| 61 | or an error occurred.
|
|---|
| 62 |
|
|---|
| 63 | The various errors returned by this module are described in the
|
|---|
| 64 | [errors section](#errors).
|
|---|
| 65 |
|
|---|
| 66 | ### bodyParser.json([options])
|
|---|
| 67 |
|
|---|
| 68 | Returns middleware that only parses `json` and only looks at requests where
|
|---|
| 69 | the `Content-Type` header matches the `type` option. This parser accepts any
|
|---|
| 70 | Unicode encoding of the body and supports automatic inflation of `gzip` and
|
|---|
| 71 | `deflate` encodings.
|
|---|
| 72 |
|
|---|
| 73 | A new `body` object containing the parsed data is populated on the `request`
|
|---|
| 74 | object after the middleware (i.e. `req.body`).
|
|---|
| 75 |
|
|---|
| 76 | #### Options
|
|---|
| 77 |
|
|---|
| 78 | The `json` function takes an optional `options` object that may contain any of
|
|---|
| 79 | the following keys:
|
|---|
| 80 |
|
|---|
| 81 | ##### inflate
|
|---|
| 82 |
|
|---|
| 83 | When set to `true`, then deflated (compressed) bodies will be inflated; when
|
|---|
| 84 | `false`, deflated bodies are rejected. Defaults to `true`.
|
|---|
| 85 |
|
|---|
| 86 | ##### limit
|
|---|
| 87 |
|
|---|
| 88 | Controls the maximum request body size. If this is a number, then the value
|
|---|
| 89 | specifies the number of bytes; if it is a string, the value is passed to the
|
|---|
| 90 | [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
|
|---|
| 91 | to `'100kb'`.
|
|---|
| 92 |
|
|---|
| 93 | ##### reviver
|
|---|
| 94 |
|
|---|
| 95 | The `reviver` option is passed directly to `JSON.parse` as the second
|
|---|
| 96 | argument. You can find more information on this argument
|
|---|
| 97 | [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).
|
|---|
| 98 |
|
|---|
| 99 | ##### strict
|
|---|
| 100 |
|
|---|
| 101 | When set to `true`, will only accept arrays and objects; when `false` will
|
|---|
| 102 | accept anything `JSON.parse` accepts. Defaults to `true`.
|
|---|
| 103 |
|
|---|
| 104 | ##### type
|
|---|
| 105 |
|
|---|
| 106 | The `type` option is used to determine what media type the middleware will
|
|---|
| 107 | parse. This option can be a string, array of strings, or a function. If not a
|
|---|
| 108 | function, `type` option is passed directly to the
|
|---|
| 109 | [type-is](https://www.npmjs.org/package/type-is#readme) library and this can
|
|---|
| 110 | be an extension name (like `json`), a mime type (like `application/json`), or
|
|---|
| 111 | a mime type with a wildcard (like `*/*` or `*/json`). If a function, the `type`
|
|---|
| 112 | option is called as `fn(req)` and the request is parsed if it returns a truthy
|
|---|
| 113 | value. Defaults to `application/json`.
|
|---|
| 114 |
|
|---|
| 115 | ##### verify
|
|---|
| 116 |
|
|---|
| 117 | The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`,
|
|---|
| 118 | where `buf` is a `Buffer` of the raw request body and `encoding` is the
|
|---|
| 119 | encoding of the request. The parsing can be aborted by throwing an error.
|
|---|
| 120 |
|
|---|
| 121 | ### bodyParser.raw([options])
|
|---|
| 122 |
|
|---|
| 123 | Returns middleware that parses all bodies as a `Buffer` and only looks at
|
|---|
| 124 | requests where the `Content-Type` header matches the `type` option. This
|
|---|
| 125 | parser supports automatic inflation of `gzip` and `deflate` encodings.
|
|---|
| 126 |
|
|---|
| 127 | A new `body` object containing the parsed data is populated on the `request`
|
|---|
| 128 | object after the middleware (i.e. `req.body`). This will be a `Buffer` object
|
|---|
| 129 | of the body.
|
|---|
| 130 |
|
|---|
| 131 | #### Options
|
|---|
| 132 |
|
|---|
| 133 | The `raw` function takes an optional `options` object that may contain any of
|
|---|
| 134 | the following keys:
|
|---|
| 135 |
|
|---|
| 136 | ##### inflate
|
|---|
| 137 |
|
|---|
| 138 | When set to `true`, then deflated (compressed) bodies will be inflated; when
|
|---|
| 139 | `false`, deflated bodies are rejected. Defaults to `true`.
|
|---|
| 140 |
|
|---|
| 141 | ##### limit
|
|---|
| 142 |
|
|---|
| 143 | Controls the maximum request body size. If this is a number, then the value
|
|---|
| 144 | specifies the number of bytes; if it is a string, the value is passed to the
|
|---|
| 145 | [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
|
|---|
| 146 | to `'100kb'`.
|
|---|
| 147 |
|
|---|
| 148 | ##### type
|
|---|
| 149 |
|
|---|
| 150 | The `type` option is used to determine what media type the middleware will
|
|---|
| 151 | parse. This option can be a string, array of strings, or a function.
|
|---|
| 152 | If not a function, `type` option is passed directly to the
|
|---|
| 153 | [type-is](https://www.npmjs.org/package/type-is#readme) library and this
|
|---|
| 154 | can be an extension name (like `bin`), a mime type (like
|
|---|
| 155 | `application/octet-stream`), or a mime type with a wildcard (like `*/*` or
|
|---|
| 156 | `application/*`). If a function, the `type` option is called as `fn(req)`
|
|---|
| 157 | and the request is parsed if it returns a truthy value. Defaults to
|
|---|
| 158 | `application/octet-stream`.
|
|---|
| 159 |
|
|---|
| 160 | ##### verify
|
|---|
| 161 |
|
|---|
| 162 | The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`,
|
|---|
| 163 | where `buf` is a `Buffer` of the raw request body and `encoding` is the
|
|---|
| 164 | encoding of the request. The parsing can be aborted by throwing an error.
|
|---|
| 165 |
|
|---|
| 166 | ### bodyParser.text([options])
|
|---|
| 167 |
|
|---|
| 168 | Returns middleware that parses all bodies as a string and only looks at
|
|---|
| 169 | requests where the `Content-Type` header matches the `type` option. This
|
|---|
| 170 | parser supports automatic inflation of `gzip` and `deflate` encodings.
|
|---|
| 171 |
|
|---|
| 172 | A new `body` string containing the parsed data is populated on the `request`
|
|---|
| 173 | object after the middleware (i.e. `req.body`). This will be a string of the
|
|---|
| 174 | body.
|
|---|
| 175 |
|
|---|
| 176 | #### Options
|
|---|
| 177 |
|
|---|
| 178 | The `text` function takes an optional `options` object that may contain any of
|
|---|
| 179 | the following keys:
|
|---|
| 180 |
|
|---|
| 181 | ##### defaultCharset
|
|---|
| 182 |
|
|---|
| 183 | Specify the default character set for the text content if the charset is not
|
|---|
| 184 | specified in the `Content-Type` header of the request. Defaults to `utf-8`.
|
|---|
| 185 |
|
|---|
| 186 | ##### inflate
|
|---|
| 187 |
|
|---|
| 188 | When set to `true`, then deflated (compressed) bodies will be inflated; when
|
|---|
| 189 | `false`, deflated bodies are rejected. Defaults to `true`.
|
|---|
| 190 |
|
|---|
| 191 | ##### limit
|
|---|
| 192 |
|
|---|
| 193 | Controls the maximum request body size. If this is a number, then the value
|
|---|
| 194 | specifies the number of bytes; if it is a string, the value is passed to the
|
|---|
| 195 | [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
|
|---|
| 196 | to `'100kb'`.
|
|---|
| 197 |
|
|---|
| 198 | ##### type
|
|---|
| 199 |
|
|---|
| 200 | The `type` option is used to determine what media type the middleware will
|
|---|
| 201 | parse. This option can be a string, array of strings, or a function. If not
|
|---|
| 202 | a function, `type` option is passed directly to the
|
|---|
| 203 | [type-is](https://www.npmjs.org/package/type-is#readme) library and this can
|
|---|
| 204 | be an extension name (like `txt`), a mime type (like `text/plain`), or a mime
|
|---|
| 205 | type with a wildcard (like `*/*` or `text/*`). If a function, the `type`
|
|---|
| 206 | option is called as `fn(req)` and the request is parsed if it returns a
|
|---|
| 207 | truthy value. Defaults to `text/plain`.
|
|---|
| 208 |
|
|---|
| 209 | ##### verify
|
|---|
| 210 |
|
|---|
| 211 | The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`,
|
|---|
| 212 | where `buf` is a `Buffer` of the raw request body and `encoding` is the
|
|---|
| 213 | encoding of the request. The parsing can be aborted by throwing an error.
|
|---|
| 214 |
|
|---|
| 215 | ### bodyParser.urlencoded([options])
|
|---|
| 216 |
|
|---|
| 217 | Returns middleware that only parses `urlencoded` bodies and only looks at
|
|---|
| 218 | requests where the `Content-Type` header matches the `type` option. This
|
|---|
| 219 | parser accepts only UTF-8 encoding of the body and supports automatic
|
|---|
| 220 | inflation of `gzip` and `deflate` encodings.
|
|---|
| 221 |
|
|---|
| 222 | A new `body` object containing the parsed data is populated on the `request`
|
|---|
| 223 | object after the middleware (i.e. `req.body`). This object will contain
|
|---|
| 224 | key-value pairs, where the value can be a string or array (when `extended` is
|
|---|
| 225 | `false`), or any type (when `extended` is `true`).
|
|---|
| 226 |
|
|---|
| 227 | #### Options
|
|---|
| 228 |
|
|---|
| 229 | The `urlencoded` function takes an optional `options` object that may contain
|
|---|
| 230 | any of the following keys:
|
|---|
| 231 |
|
|---|
| 232 | ##### extended
|
|---|
| 233 |
|
|---|
| 234 | The `extended` option allows to choose between parsing the URL-encoded data
|
|---|
| 235 | with the `querystring` library (when `false`) or the `qs` library (when
|
|---|
| 236 | `true`). The "extended" syntax allows for rich objects and arrays to be
|
|---|
| 237 | encoded into the URL-encoded format, allowing for a JSON-like experience
|
|---|
| 238 | with URL-encoded. For more information, please
|
|---|
| 239 | [see the qs library](https://www.npmjs.org/package/qs#readme).
|
|---|
| 240 |
|
|---|
| 241 | Defaults to `true`, but using the default has been deprecated. Please
|
|---|
| 242 | research into the difference between `qs` and `querystring` and choose the
|
|---|
| 243 | appropriate setting.
|
|---|
| 244 |
|
|---|
| 245 | ##### inflate
|
|---|
| 246 |
|
|---|
| 247 | When set to `true`, then deflated (compressed) bodies will be inflated; when
|
|---|
| 248 | `false`, deflated bodies are rejected. Defaults to `true`.
|
|---|
| 249 |
|
|---|
| 250 | ##### limit
|
|---|
| 251 |
|
|---|
| 252 | Controls the maximum request body size. If this is a number, then the value
|
|---|
| 253 | specifies the number of bytes; if it is a string, the value is passed to the
|
|---|
| 254 | [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
|
|---|
| 255 | to `'100kb'`.
|
|---|
| 256 |
|
|---|
| 257 | ##### parameterLimit
|
|---|
| 258 |
|
|---|
| 259 | The `parameterLimit` option controls the maximum number of parameters that
|
|---|
| 260 | are allowed in the URL-encoded data. If a request contains more parameters
|
|---|
| 261 | than this value, a 413 will be returned to the client. Defaults to `1000`.
|
|---|
| 262 |
|
|---|
| 263 | ##### type
|
|---|
| 264 |
|
|---|
| 265 | The `type` option is used to determine what media type the middleware will
|
|---|
| 266 | parse. This option can be a string, array of strings, or a function. If not
|
|---|
| 267 | a function, `type` option is passed directly to the
|
|---|
| 268 | [type-is](https://www.npmjs.org/package/type-is#readme) library and this can
|
|---|
| 269 | be an extension name (like `urlencoded`), a mime type (like
|
|---|
| 270 | `application/x-www-form-urlencoded`), or a mime type with a wildcard (like
|
|---|
| 271 | `*/x-www-form-urlencoded`). If a function, the `type` option is called as
|
|---|
| 272 | `fn(req)` and the request is parsed if it returns a truthy value. Defaults
|
|---|
| 273 | to `application/x-www-form-urlencoded`.
|
|---|
| 274 |
|
|---|
| 275 | ##### verify
|
|---|
| 276 |
|
|---|
| 277 | The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`,
|
|---|
| 278 | where `buf` is a `Buffer` of the raw request body and `encoding` is the
|
|---|
| 279 | encoding of the request. The parsing can be aborted by throwing an error.
|
|---|
| 280 |
|
|---|
| 281 | #### depth
|
|---|
| 282 |
|
|---|
| 283 | The `depth` option is used to configure the maximum depth of the `qs` library when `extended` is `true`. This allows you to limit the amount of keys that are parsed and can be useful to prevent certain types of abuse. Defaults to `32`. It is recommended to keep this value as low as possible.
|
|---|
| 284 |
|
|---|
| 285 | ## Errors
|
|---|
| 286 |
|
|---|
| 287 | The middlewares provided by this module create errors using the
|
|---|
| 288 | [`http-errors` module](https://www.npmjs.com/package/http-errors). The errors
|
|---|
| 289 | will typically have a `status`/`statusCode` property that contains the suggested
|
|---|
| 290 | HTTP response code, an `expose` property to determine if the `message` property
|
|---|
| 291 | should be displayed to the client, a `type` property to determine the type of
|
|---|
| 292 | error without matching against the `message`, and a `body` property containing
|
|---|
| 293 | the read body, if available.
|
|---|
| 294 |
|
|---|
| 295 | The following are the common errors created, though any error can come through
|
|---|
| 296 | for various reasons.
|
|---|
| 297 |
|
|---|
| 298 | ### content encoding unsupported
|
|---|
| 299 |
|
|---|
| 300 | This error will occur when the request had a `Content-Encoding` header that
|
|---|
| 301 | contained an encoding but the "inflation" option was set to `false`. The
|
|---|
| 302 | `status` property is set to `415`, the `type` property is set to
|
|---|
| 303 | `'encoding.unsupported'`, and the `charset` property will be set to the
|
|---|
| 304 | encoding that is unsupported.
|
|---|
| 305 |
|
|---|
| 306 | ### entity parse failed
|
|---|
| 307 |
|
|---|
| 308 | This error will occur when the request contained an entity that could not be
|
|---|
| 309 | parsed by the middleware. The `status` property is set to `400`, the `type`
|
|---|
| 310 | property is set to `'entity.parse.failed'`, and the `body` property is set to
|
|---|
| 311 | the entity value that failed parsing.
|
|---|
| 312 |
|
|---|
| 313 | ### entity verify failed
|
|---|
| 314 |
|
|---|
| 315 | This error will occur when the request contained an entity that could not be
|
|---|
| 316 | failed verification by the defined `verify` option. The `status` property is
|
|---|
| 317 | set to `403`, the `type` property is set to `'entity.verify.failed'`, and the
|
|---|
| 318 | `body` property is set to the entity value that failed verification.
|
|---|
| 319 |
|
|---|
| 320 | ### request aborted
|
|---|
| 321 |
|
|---|
| 322 | This error will occur when the request is aborted by the client before reading
|
|---|
| 323 | the body has finished. The `received` property will be set to the number of
|
|---|
| 324 | bytes received before the request was aborted and the `expected` property is
|
|---|
| 325 | set to the number of expected bytes. The `status` property is set to `400`
|
|---|
| 326 | and `type` property is set to `'request.aborted'`.
|
|---|
| 327 |
|
|---|
| 328 | ### request entity too large
|
|---|
| 329 |
|
|---|
| 330 | This error will occur when the request body's size is larger than the "limit"
|
|---|
| 331 | option. The `limit` property will be set to the byte limit and the `length`
|
|---|
| 332 | property will be set to the request body's length. The `status` property is
|
|---|
| 333 | set to `413` and the `type` property is set to `'entity.too.large'`.
|
|---|
| 334 |
|
|---|
| 335 | ### request size did not match content length
|
|---|
| 336 |
|
|---|
| 337 | This error will occur when the request's length did not match the length from
|
|---|
| 338 | the `Content-Length` header. This typically occurs when the request is malformed,
|
|---|
| 339 | typically when the `Content-Length` header was calculated based on characters
|
|---|
| 340 | instead of bytes. The `status` property is set to `400` and the `type` property
|
|---|
| 341 | is set to `'request.size.invalid'`.
|
|---|
| 342 |
|
|---|
| 343 | ### stream encoding should not be set
|
|---|
| 344 |
|
|---|
| 345 | This error will occur when something called the `req.setEncoding` method prior
|
|---|
| 346 | to this middleware. This module operates directly on bytes only and you cannot
|
|---|
| 347 | call `req.setEncoding` when using this module. The `status` property is set to
|
|---|
| 348 | `500` and the `type` property is set to `'stream.encoding.set'`.
|
|---|
| 349 |
|
|---|
| 350 | ### stream is not readable
|
|---|
| 351 |
|
|---|
| 352 | This error will occur when the request is no longer readable when this middleware
|
|---|
| 353 | attempts to read it. This typically means something other than a middleware from
|
|---|
| 354 | this module read the request body already and the middleware was also configured to
|
|---|
| 355 | read the same request. The `status` property is set to `500` and the `type`
|
|---|
| 356 | property is set to `'stream.not.readable'`.
|
|---|
| 357 |
|
|---|
| 358 | ### too many parameters
|
|---|
| 359 |
|
|---|
| 360 | This error will occur when the content of the request exceeds the configured
|
|---|
| 361 | `parameterLimit` for the `urlencoded` parser. The `status` property is set to
|
|---|
| 362 | `413` and the `type` property is set to `'parameters.too.many'`.
|
|---|
| 363 |
|
|---|
| 364 | ### unsupported charset "BOGUS"
|
|---|
| 365 |
|
|---|
| 366 | This error will occur when the request had a charset parameter in the
|
|---|
| 367 | `Content-Type` header, but the `iconv-lite` module does not support it OR the
|
|---|
| 368 | parser does not support it. The charset is contained in the message as well
|
|---|
| 369 | as in the `charset` property. The `status` property is set to `415`, the
|
|---|
| 370 | `type` property is set to `'charset.unsupported'`, and the `charset` property
|
|---|
| 371 | is set to the charset that is unsupported.
|
|---|
| 372 |
|
|---|
| 373 | ### unsupported content encoding "bogus"
|
|---|
| 374 |
|
|---|
| 375 | This error will occur when the request had a `Content-Encoding` header that
|
|---|
| 376 | contained an unsupported encoding. The encoding is contained in the message
|
|---|
| 377 | as well as in the `encoding` property. The `status` property is set to `415`,
|
|---|
| 378 | the `type` property is set to `'encoding.unsupported'`, and the `encoding`
|
|---|
| 379 | property is set to the encoding that is unsupported.
|
|---|
| 380 |
|
|---|
| 381 | ### The input exceeded the depth
|
|---|
| 382 |
|
|---|
| 383 | This error occurs when using `bodyParser.urlencoded` with the `extended` property set to `true` and the input exceeds the configured `depth` option. The `status` property is set to `400`. It is recommended to review the `depth` option and evaluate if it requires a higher value. When the `depth` option is set to `32` (default value), the error will not be thrown.
|
|---|
| 384 |
|
|---|
| 385 | ## Examples
|
|---|
| 386 |
|
|---|
| 387 | ### Express/Connect top-level generic
|
|---|
| 388 |
|
|---|
| 389 | This example demonstrates adding a generic JSON and URL-encoded parser as a
|
|---|
| 390 | top-level middleware, which will parse the bodies of all incoming requests.
|
|---|
| 391 | This is the simplest setup.
|
|---|
| 392 |
|
|---|
| 393 | ```js
|
|---|
| 394 | var express = require('express')
|
|---|
| 395 | var bodyParser = require('body-parser')
|
|---|
| 396 |
|
|---|
| 397 | var app = express()
|
|---|
| 398 |
|
|---|
| 399 | // parse application/x-www-form-urlencoded
|
|---|
| 400 | app.use(bodyParser.urlencoded({ extended: false }))
|
|---|
| 401 |
|
|---|
| 402 | // parse application/json
|
|---|
| 403 | app.use(bodyParser.json())
|
|---|
| 404 |
|
|---|
| 405 | app.use(function (req, res) {
|
|---|
| 406 | res.setHeader('Content-Type', 'text/plain')
|
|---|
| 407 | res.write('you posted:\n')
|
|---|
| 408 | res.end(JSON.stringify(req.body, null, 2))
|
|---|
| 409 | })
|
|---|
| 410 | ```
|
|---|
| 411 |
|
|---|
| 412 | ### Express route-specific
|
|---|
| 413 |
|
|---|
| 414 | This example demonstrates adding body parsers specifically to the routes that
|
|---|
| 415 | need them. In general, this is the most recommended way to use body-parser with
|
|---|
| 416 | Express.
|
|---|
| 417 |
|
|---|
| 418 | ```js
|
|---|
| 419 | var express = require('express')
|
|---|
| 420 | var bodyParser = require('body-parser')
|
|---|
| 421 |
|
|---|
| 422 | var app = express()
|
|---|
| 423 |
|
|---|
| 424 | // create application/json parser
|
|---|
| 425 | var jsonParser = bodyParser.json()
|
|---|
| 426 |
|
|---|
| 427 | // create application/x-www-form-urlencoded parser
|
|---|
| 428 | var urlencodedParser = bodyParser.urlencoded({ extended: false })
|
|---|
| 429 |
|
|---|
| 430 | // POST /login gets urlencoded bodies
|
|---|
| 431 | app.post('/login', urlencodedParser, function (req, res) {
|
|---|
| 432 | res.send('welcome, ' + req.body.username)
|
|---|
| 433 | })
|
|---|
| 434 |
|
|---|
| 435 | // POST /api/users gets JSON bodies
|
|---|
| 436 | app.post('/api/users', jsonParser, function (req, res) {
|
|---|
| 437 | // create user in req.body
|
|---|
| 438 | })
|
|---|
| 439 | ```
|
|---|
| 440 |
|
|---|
| 441 | ### Change accepted type for parsers
|
|---|
| 442 |
|
|---|
| 443 | All the parsers accept a `type` option which allows you to change the
|
|---|
| 444 | `Content-Type` that the middleware will parse.
|
|---|
| 445 |
|
|---|
| 446 | ```js
|
|---|
| 447 | var express = require('express')
|
|---|
| 448 | var bodyParser = require('body-parser')
|
|---|
| 449 |
|
|---|
| 450 | var app = express()
|
|---|
| 451 |
|
|---|
| 452 | // parse various different custom JSON types as JSON
|
|---|
| 453 | app.use(bodyParser.json({ type: 'application/*+json' }))
|
|---|
| 454 |
|
|---|
| 455 | // parse some custom thing into a Buffer
|
|---|
| 456 | app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
|
|---|
| 457 |
|
|---|
| 458 | // parse an HTML body into a string
|
|---|
| 459 | app.use(bodyParser.text({ type: 'text/html' }))
|
|---|
| 460 | ```
|
|---|
| 461 |
|
|---|
| 462 | ## License
|
|---|
| 463 |
|
|---|
| 464 | [MIT](LICENSE)
|
|---|
| 465 |
|
|---|
| 466 | [ci-image]: https://badgen.net/github/checks/expressjs/body-parser/master?label=ci
|
|---|
| 467 | [ci-url]: https://github.com/expressjs/body-parser/actions/workflows/ci.yml
|
|---|
| 468 | [coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/body-parser/master
|
|---|
| 469 | [coveralls-url]: https://coveralls.io/r/expressjs/body-parser?branch=master
|
|---|
| 470 | [node-version-image]: https://badgen.net/npm/node/body-parser
|
|---|
| 471 | [node-version-url]: https://nodejs.org/en/download
|
|---|
| 472 | [npm-downloads-image]: https://badgen.net/npm/dm/body-parser
|
|---|
| 473 | [npm-url]: https://npmjs.org/package/body-parser
|
|---|
| 474 | [npm-version-image]: https://badgen.net/npm/v/body-parser
|
|---|
| 475 | [ossf-scorecard-badge]: https://api.scorecard.dev/projects/github.com/expressjs/body-parser/badge
|
|---|
| 476 | [ossf-scorecard-visualizer]: https://ossf.github.io/scorecard-visualizer/#/projects/github.com/expressjs/body-parser |
|---|