Get and validate the raw body of a readable stream.
Gets the entire buffer of a stream either as a
Buffer or a string.
Validates the stream's length against an expected length and maximum limit.
Ideal for parsing request bodies.
var getRawBody =
Returns a promise if no callback specified and global
length- The length of the stream. If the contents of the stream do not add up to this length, an
400error code is returned.
limit- The byte limit of the body. This is the number of bytes or any string format supported by bytes, for example
'3mb'. If the body ends up being larger than this limit, a
413error code is returned.
encoding- The encoding to use to decode the body into a string. By default, a
Bufferinstance will be returned when no encoding is specified. Most likely, you want
utf-8, so setting
truewill decode as
utf-8. You can use any type of encoding supported by iconv-lite.
You can also pass a string in place of options to just specify the encoding.
err - the following attributes will be defined if applicable:
limit- the limit in bytes
expected- the expected length of the stream
received- the received bytes
encoding- the invalid encoding
statusCode- the corresponding status code for the error
res - the result, either as a
String if an encoding was set or a
If an error occurs, the stream will be paused, everything unpiped,
and you are responsible for correctly disposing the stream.
For HTTP requests, no handling is required if you send a response.
For streams that use file descriptors, you should
stream.close() to prevent leaks.
var contentType =var express =var getRawBody =var app =app// now access req.text
var contentType =var getRawBody =var koa =var app =app// now access this.text
To use this library as a promise, simply omit the
callback and a promise is
returned, provided that a global
Promise is defined.
var getRawBody =var http =var server = httpserver