middleware-body
HTTP request body parser middleware for Marble.js.
Last updated
HTTP request body parser middleware for Marble.js.
Last updated
Requires @marblejs/core
to be installed.
BodyParserOptions
The type
option is used to determine what media type the middleware will parse. It is passed directly to the type-is library and this can be an extension name (like json
), a mime type (like application/json
), or a mime type with a wildcard (like */*
or */json
). Defaults to */*
.
Lets assume that we have the following CURL command, which triggers POST /api/login
endpoint:
Using previously connected bodyParser$
middleware, the app will intercept the following payload object:
The POST request body can be intercepted like follows.
All properties and values in req.body
object are untrusted (unknown) and should be validated before trusting.
This middleware does not handle multipart bodies.
The middleware does nothing if request Content-Type is not matched, which makes a possibility for chaining multiple parsers one after another. For example, we can register multiple middlewares that will parse only a certain set of possible Content-Type headers.
Default parser:
Parses application/json (to JSON), application/x-www-form-urlencoded (to JSON), application/octet-stream (to Buffer) and text/plain (to string) content types
JSON parser:
Parses req.body
to JSON object.
URLEncoded parser:
Parses encoded URLs req.body
to JSON object.
Text parser:
Parses req.body
to string.
Raw parser:
Parses req.body
to Buffer.
If the available parsers are not enough, you can create your own body parsers by conforming to RequestBodyParser
interface. The example below shows how the jsonParser
looks underneath.
parameter
definition
options
<optional> BodyParserOptions
parameter
definition
parser
<optional> RequestBodyParser
type
<optional> Array<string>