Skip to content
Node.js body parsing middleware
Branch: master
Clone or download
Pull request Compare This branch is 3 commits ahead, 348 commits behind expressjs:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.gitignore
.npmignore
.travis.yml
HISTORY.md
LICENSE
README.md
index.js
package.json

README.md

body-parser

NPM version Build Status Coverage Status

Node.js body parsing middleware.

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:

Other body parsers you might be interested in:

Installation

$ npm install body-parser

API

var express    = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

// parse application/vnd.api+json as json
app.use(bodyParser.json({ type: 'application/vnd.api+json' }))

// parse application/json into req.json
app.use(bodyParser.json({ into: 'json' }))

app.use(function (req, res, next) {
  console.log(req.body) // populated!
  next()
})

bodyParser.json(options)

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:

  • 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
  • into - the name of the property to create on req

The type argument is passed directly to the type-is 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 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.

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.

bodyParser.raw(options)

Returns middleware that parses all bodies as a Buffer. This parser supports automatic inflation of gzip and deflate encodings.

The options are:

  • 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
  • into - the name of the property to create on req

The type argument is passed directly to the type-is 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 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.

bodyParser.text(options)

Returns middleware that parses all bodies as a string. This parser supports automatic inflation of gzip and deflate encodings.

The options are:

  • 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
  • into - the name of the property to create on req

The type argument is passed directly to the type-is 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 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.

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.

The options are:

  • extended - parse extended syntax with the qs module. (default: true)
  • inflate - if deflated bodies will be inflated. (default: true)
  • limit - maximum request body size. (default: <100kb>)
  • type - request content-type to parse (default: urlencoded)
  • verify - function to verify body content
  • into - the name of the property to create on req

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.

The type argument is passed directly to the type-is 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 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.

req.body

A new body object containing the parsed data is populated on the request object after the middleware by default, unless the into option is set in which case request[into] is populated with the parsed data.

License

MIT

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.