Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @middy/http-json-body-parser with all npm packages installed. Try it out:

require("@middy/core/package.json"); // @middy/core is a peer dependency. var httpJsonBodyParser = require("@middy/http-json-body-parser")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@middy/http-json-body-parser v1.1.0

Http JSON body parser middleware for the middy framework

Middy http-json-body-parser middleware

Middy logo

HTTP json body parser middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda

This middleware automatically parses HTTP requests with a JSON body and converts the body into an object. Also handles gracefully broken JSON as UnprocessableEntity (422 errors) if used in combination with httpErrorHandler.

It can also be used in combination with validator as a prior step to normalize the event body input as an object so that the content can be validated.

Install

To install this middleware you can use NPM:

npm install --save @middy/http-json-body-parser

Options

  • reviver (function) (optional): A reviver parameter may be passed which will be used JSON.parseing the body.

Sample usage

const middy = require('@middy/core')
const httpJsonBodyParser = require('@middy/http-json-body-parser')

const handler = middy((event, context, cb) => {
  cb(null, {})
})

handler.use(httpJsonBodyParser())

// invokes the handler
const event = {
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({foo: 'bar'})
}
handler(event, {}, (_, body) => {
  expect(body).toEqual({foo: 'bar'})
})

Middy documentation and examples

For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.

Contributing

Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.

License

Licensed under MIT License. Copyright (c) 2017-2018 Luciano Mammino and the Middy team.

FOSSA Status

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free