Parse incoming request bodies in a middleware before your handlers, available under the req.body
property.
This does not handle multipart bodies, due to their complex and typically large nature.
This module provides the following parsers:
lit install voronianski/body-parser
local bodyParser = require('body-parser')
The bodyParser table exposes various factories to create middlewares. All middlewares will populate the req.body
property with the parsed body or an empty table ({}
) if there was no body to parse (or an error was returned).
Returns middleware that only parses json
.
The function takes an options
table that may contain any of
the following keys:
-
limit
- controls the maximum request body size, specifies the number of bytes. Defaults to100000
(~100Kb
) -
strict
- when set totrue
, will only accept arrays and objects; whenfalse
will accept anythingJSON.parse
accepts. Defaults totrue
. -
type
- thetype
option is used to determine what media type the middleware will parse. Defaults toapplication/json
.
Returns middleware that only parses urlencoded bodies.
The function takes an options
table that may contain any of
the following keys:
-
limit
- controls the maximum request body size, specifies the number of bytes. Defaults to100000
(~100Kb
) -
type
- thetype
option is used to determine what media type the middleware will parse. Defaults toapplication/x-www-form-urlencoded
.
Returns middleware that parses all bodies as a string.
The function takes an options
table that may contain any of
the following keys:
-
limit
- controls the maximum request body size, specifies the number of bytes. Defaults to100000
(~100Kb
) -
type
- thetype
option is used to determine what media type the middleware will parse. Defaults totext/plain
.
Returns middleware that parses all bodies as a Buffer
.
The function takes an options
table that may contain any of
the following keys:
-
limit
- controls the maximum request body size, specifies the number of bytes. Defaults to100000
(~100Kb
) -
type
- thetype
option is used to determine what media type the middleware will parse. Defaults toapplication/octet-stream
.
This example demonstrates adding a generic JSON and URL-encoded parser as a top-level middleware, which will parse the bodies of all incoming requests. This is the simplest setup.
local json = requrie('json')
local Utopia = require('utopia')
local bodyParser = require('body-parser')
local app = Utopia:new()
-- parse application/x-www-form-urlencoded
app:use(bodyParser.urlencoded())
-- parse application/json
app:use(bodyParser.json())
app:use(function (req, res)
res:finish('you posted: ' .. json.stringify(req.body, {indent = 2}))
end)
app:listen(3000)
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'}))
MIT Licensed
Copyright (c) 2016 Dmitri Voronianski dmitri.voronianski@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.