npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

body-parser-json

v1.9.2

Published

Node.js body parsing middleware

Downloads

90

Readme

body-parser

NPM Version NPM Downloads Build Status Test Coverage Gratipay

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 bodyParser = require('body-parser')

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

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

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

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>)
  • parameterLimit - maximum number of parameters. (default: 1000)
  • type - request content-type to parse (default: urlencoded)
  • verify - function to verify body content

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 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.

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.

Examples

express/connect top-level generic

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.

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())

app.use(function (req, res) {
  res.setHeader('Content-Type', 'text/plain')
  res.write('you posted:\n')
  res.end(JSON.stringify(req.body, null, 2))
})

express route-specific

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.

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

var app = express()

// create application/json parser
var jsonParser = bodyParser.json()

// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

// POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {
  if (!req.body) return res.sendStatus(400)
  res.send('welcome, ' + res.body.username)
})

// POST /api/users gets JSON bodies
app.post('/api/users', jsonParser, function (req, res) {
  if (!req.body) return res.sendStatus(400)
  // create user in req.body
})

change content-type for parsers

All the parsers accept a type option which allows you to change the Content-Type that the middleware will parse.

// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))

// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))

// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }))

License

MIT