\href{https://npmjs.org/package/raw-body}{\texttt{ }} \href{https://npmjs.org/package/raw-body}{\texttt{ }} \href{https://nodejs.org/en/download/}{\texttt{ }} \href{https://travis-ci.org/stream-utils/raw-body}{\texttt{ }} \href{https://coveralls.io/r/stream-utils/raw-body?branch=master}{\texttt{ }}

Gets the entire buffer of a stream either as a {\ttfamily Buffer} or a string. Validates the stream\textquotesingle{}s length against an expected length and maximum limit. Ideal for parsing request bodies.\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23874}{}\doxysection{Install}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23874}
This is a \href{https://nodejs.org/en/}{\texttt{ Node.\+js}} module available through the \href{https://www.npmjs.com/}{\texttt{ npm registry}}. Installation is done using the \href{https://docs.npmjs.com/getting-started/installing-npm-packages-locally}{\texttt{ {\ttfamily npm install} command}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install raw-\/body }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23875}{}\doxysubsection{Type\+Script}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23875}
This module includes a \href{https://www.typescriptlang.org/}{\texttt{ Type\+Script}} declaration file to enable auto complete in compatible editors and type information for Type\+Script projects. This module depends on the Node.\+js types, so install {\ttfamily @types/node}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install @types/node }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23876}{}\doxysection{API}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23876}

\begin{DoxyCode}{0}
\DoxyCodeLine{var getRawBody = require('raw-\/body') }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23877}{}\doxysubsection{get\+Raw\+Body(stream, \mbox{[}options\mbox{]}, \mbox{[}callback\mbox{]})}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23877}
{\bfseries{Returns a promise if no callback specified and global {\ttfamily Promise} exists.}}

Options\+:


\begin{DoxyItemize}
\item {\ttfamily length} -\/ The length of the stream. If the contents of the stream do not add up to this length, an {\ttfamily 400} error code is returned.
\item {\ttfamily limit} -\/ The byte limit of the body. This is the number of bytes or any string format supported by \href{https://www.npmjs.com/package/bytes}{\texttt{ bytes}}, for example {\ttfamily 1000}, `\textquotesingle{}500kb'{\ttfamily or}\textquotesingle{}3mb\textquotesingle{}\`{}. If the body ends up being larger than this limit, a {\ttfamily 413} error code is returned.
\item {\ttfamily encoding} -\/ The encoding to use to decode the body into a string. By default, a {\ttfamily Buffer} instance will be returned when no encoding is specified. Most likely, you want {\ttfamily utf-\/8}, so setting {\ttfamily encoding} to {\ttfamily true} will decode as {\ttfamily utf-\/8}. You can use any type of encoding supported by \href{https://www.npmjs.org/package/iconv-lite\#readme}{\texttt{ iconv-\/lite}}.
\end{DoxyItemize}

You can also pass a string in place of options to just specify the encoding.

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 {\ttfamily stream.\+destroy()} or {\ttfamily stream.\+close()} to prevent leaks.\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23878}{}\doxysection{Errors}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23878}
This module creates errors depending on the error condition during reading. The error may be an error from the underlying Node.\+js implementation, but is otherwise an error created by this module, which has the following attributes\+:


\begin{DoxyItemize}
\item {\ttfamily limit} -\/ the limit in bytes
\item {\ttfamily length} and {\ttfamily expected} -\/ the expected length of the stream
\item {\ttfamily received} -\/ the received bytes
\item {\ttfamily encoding} -\/ the invalid encoding
\item {\ttfamily status} and {\ttfamily status\+Code} -\/ the corresponding status code for the error
\item {\ttfamily type} -\/ the error type
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23879}{}\doxysubsection{Types}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23879}
The errors from this module have a {\ttfamily type} property which allows for the progamatic determination of the type of error returned.\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23880}{}\doxysubsubsection{encoding.\+unsupported}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23880}
This error will occur when the {\ttfamily encoding} option is specified, but the value does not map to an encoding supported by the \href{https://www.npmjs.org/package/iconv-lite\#readme}{\texttt{ iconv-\/lite}} module.\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23881}{}\doxysubsubsection{entity.\+too.\+large}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23881}
This error will occur when the {\ttfamily limit} option is specified, but the stream has an entity that is larger.\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23882}{}\doxysubsubsection{request.\+aborted}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23882}
This error will occur when the request stream is aborted by the client before reading the body has finished.\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23883}{}\doxysubsubsection{request.\+size.\+invalid}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23883}
This error will occur when the {\ttfamily length} option is specified, but the stream has emitted more bytes.\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23884}{}\doxysubsubsection{stream.\+encoding.\+set}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23884}
This error will occur when the given stream has an encoding set on it, making it a decoded stream. The stream should not have an encoding set and is expected to emit {\ttfamily Buffer} objects.\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23885}{}\doxysection{Examples}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23885}
\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23886}{}\doxysubsection{Simple Express example}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23886}

\begin{DoxyCode}{0}
\DoxyCodeLine{var contentType = require('content-\/type')}
\DoxyCodeLine{var express = require('express')}
\DoxyCodeLine{var getRawBody = require('raw-\/body')}
\DoxyCodeLine{}
\DoxyCodeLine{var app = express()}
\DoxyCodeLine{}
\DoxyCodeLine{app.use(function (req, res, next) \{}
\DoxyCodeLine{  getRawBody(req, \{}
\DoxyCodeLine{    length: req.headers['content-\/length'],}
\DoxyCodeLine{    limit: '1mb',}
\DoxyCodeLine{    encoding: contentType.parse(req).parameters.charset}
\DoxyCodeLine{  \}, function (err, string) \{}
\DoxyCodeLine{    if (err) return next(err)}
\DoxyCodeLine{    req.text = string}
\DoxyCodeLine{    next()}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{// now access req.text }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23887}{}\doxysubsection{Simple Koa example}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23887}

\begin{DoxyCode}{0}
\DoxyCodeLine{var contentType = require('content-\/type')}
\DoxyCodeLine{var getRawBody = require('raw-\/body')}
\DoxyCodeLine{var koa = require('koa')}
\DoxyCodeLine{}
\DoxyCodeLine{var app = koa()}
\DoxyCodeLine{}
\DoxyCodeLine{app.use(function * (next) \{}
\DoxyCodeLine{  this.text = yield getRawBody(this.req, \{}
\DoxyCodeLine{    length: this.req.headers['content-\/length'],}
\DoxyCodeLine{    limit: '1mb',}
\DoxyCodeLine{    encoding: contentType.parse(this.req).parameters.charset}
\DoxyCodeLine{  \})}
\DoxyCodeLine{  yield next}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{// now access this.text }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23888}{}\doxysubsection{Using as a promise}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23888}
To use this library as a promise, simply omit the {\ttfamily callback} and a promise is returned, provided that a global {\ttfamily Promise} is defined.


\begin{DoxyCode}{0}
\DoxyCodeLine{var getRawBody = require('raw-\/body')}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{}
\DoxyCodeLine{var server = http.createServer(function (req, res) \{}
\DoxyCodeLine{  getRawBody(req)}
\DoxyCodeLine{    .then(function (buf) \{}
\DoxyCodeLine{      res.statusCode = 200}
\DoxyCodeLine{      res.end(buf.length + ' bytes submitted')}
\DoxyCodeLine{    \})}
\DoxyCodeLine{    .catch(function (err) \{}
\DoxyCodeLine{      res.statusCode = 500}
\DoxyCodeLine{      res.end(err.message)}
\DoxyCodeLine{    \})}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23889}{}\doxysubsection{Using with Type\+Script}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23889}

\begin{DoxyCode}{0}
\DoxyCodeLine{import * as getRawBody from 'raw-\/body';}
\DoxyCodeLine{import * as http from 'http';}
\DoxyCodeLine{}
\DoxyCodeLine{const server = http.createServer((req, res) => \{}
\DoxyCodeLine{  getRawBody(req)}
\DoxyCodeLine{  .then((buf) => \{}
\DoxyCodeLine{    res.statusCode = 200;}
\DoxyCodeLine{    res.end(buf.length + ' bytes submitted');}
\DoxyCodeLine{  \})}
\DoxyCodeLine{  .catch((err) => \{}
\DoxyCodeLine{    res.statusCode = err.statusCode;}
\DoxyCodeLine{    res.end(err.message);}
\DoxyCodeLine{  \});}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_raw_body_README_autotoc_md23890}{}\doxysection{License}\label{md_sfcode_node_modules_raw_body_README_autotoc_md23890}
\mbox{[}MIT\mbox{]}(LICENSE) 