reserve

body helper

🔝 REserve documentation

REserve offers a method to deserialize a request body.

interface BodyOptions {
  ignoreContentLength?: boolean
}

type BodyResult = Promise<Buffer | string | object> & {
  buffer: () => Promise<Buffer>
  text: () => Promise<string>
  json: () => Promise<object>
}

function body (request: IncomingMessage, options?: BodyOptions): BodyResult

Types definition for body

import { body } from 'reserve'

async function customHandler (request, response) {
  const requestBody = await body(request).json()
  /* ... */
}

Example of body

The return of await body(request) depends on the request headers.

If the content-type is specified and starts with :

It is possible to force the return type using :

[!CAUTION] If the request’s content-length is set (and not ignored through the ignoreContentLength option), the buffer is allocated accordingly.

It means the result might be truncated (if too small) or padded with \x00 (if too large).