# co-body

> request body parsing for co

Latest version **6.2.0** (published 2024-06-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install co-body
pnpm add co-body
yarn add co-body
bun add co-body
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.2.0 |
| Published | 2024-06-05 |
| First published | 2013-11-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/co-body) |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 5 |
| Unpacked size | 10.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 325 |
| Maintainers | dead_horse, tjholowaychuk, fengmk2 |
| Keywords | request, parse, parser, json, co, generators, urlencoded |

## Links

- npm: https://www.npmjs.com/package/co-body
- Repository: https://github.com/cojs/co-body
- Homepage: https://github.com/cojs/co-body#readme
- Issues: https://github.com/cojs/co-body/issues
- npm.io page: https://npm.io/package/co-body

## Dependencies (5)

- [qs](https://npm.io/package/qs.md) ^6.5.2
- [type-is](https://npm.io/package/type-is.md) ^1.6.16
- [raw-body](https://npm.io/package/raw-body.md) ^2.3.3
- [inflation](https://npm.io/package/inflation.md) ^2.0.0
- [@hapi/bourne](https://npm.io/package/@hapi/bourne.md) ^3.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 6.2.0 (latest) — 2024-06-05
- 6.1.0 — 2020-10-08
- 6.0.0 — 2018-05-21
- 5.2.0 — 2018-05-02
- 5.1.1 — 2017-03-24
- 5.1.0 — 2017-03-21
- 5.0.3 — 2017-03-19
- 5.0.2 — 2017-03-10
- 5.0.1 — 2017-03-06
- 5.0.0 — 2017-03-01
- 4.2.0 — 2016-05-04
- 4.1.0 — 2016-05-04
- 4.0.0 — 2015-08-14
- 3.1.0 — 2015-08-05
- 3.0.0 — 2015-07-25
- … 6 more at https://npm.io/package/co-body/versions

## README

# co-body

[![NPM version][npm-image]][npm-url]
[![CI](https://github.com/cojs/co-body/actions/workflows/node.yml/badge.svg)](https://github.com/cojs/co-body/actions/workflows/node.yml)
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/co-body.svg?style=flat-square
[npm-url]: https://npmjs.org/package/co-body
[codecov-image]: https://codecov.io/github/cojs/co-body/coverage.svg?branch=master
[codecov-url]: https://codecov.io/github/cojs/co-body?branch=master
[download-image]: https://img.shields.io/npm/dm/co-body.svg?style=flat-square
[download-url]: https://npmjs.org/package/co-body

> Parse request bodies with generators inspired by [Raynos/body](https://github.com/Raynos/body).

## Installation

```bash
$ npm install co-body
```

## Options

  - `limit` number or string representing the request size limit (1mb for json and 56kb for form-urlencoded)
  - `strict` when set to `true`, JSON parser will only accept arrays and objects; when `false` will accept anything `JSON.parse` accepts. Defaults to `true`. (also `strict` mode will always return object).
  - `onProtoPoisoning` Defines what action the `co-body` lib must take when parsing a JSON object with `__proto__`. This functionality is provided by [bourne](https://github.com/hapijs/bourne).
    See [Prototype-Poisoning](https://fastify.dev/docs/latest/Guides/Prototype-Poisoning/) for more details about prototype poisoning attacks.
    Possible values are `'error'`, `'remove'` and `'ignore'`.
    Default to `'error'`, it will throw a `SyntaxError` when `Prototype-Poisoning` happen.
  - `queryString` an object of options when parsing query strings and form data. See [qs](https://github.com/hapijs/qs) for more information.
  - `returnRawBody` when set to `true`, the return value of `co-body` will be an object with two properties: `{ parsed: /* parsed value */, raw: /* raw body */}`.
  - `jsonTypes` is used to determine what media type **co-body** will parse as **json**, this option is passed directly to the [type-is](https://github.com/jshttp/type-is) library.
  - `formTypes` is used to determine what media type **co-body** will parse as **form**, this option is passed directly to the [type-is](https://github.com/jshttp/type-is) library.
  - `textTypes` is used to determine what media type **co-body** will parse as **text**, this option is passed directly to the [type-is](https://github.com/jshttp/type-is) library.

more options available via [raw-body](https://github.com/stream-utils/raw-body#getrawbodystream-options-callback):

## Example

```js
// application/json
var body = await parse.json(req);

// explicit limit
var body = await parse.json(req, { limit: '10kb' });

// application/x-www-form-urlencoded
var body = await parse.form(req);

// text/plain
var body = await parse.text(req);

// either
var body = await parse(req);

// custom type
var body = await parse(req, { textTypes: ['text', 'html'] });
```

## Koa

  This lib also supports `ctx.req` in Koa (or other libraries),
  so that you may simply use `this` instead of `this.req`.

```js
// application/json
var body = await parse.json(this);

// application/x-www-form-urlencoded
var body = await parse.form(this);

// text/plain
var body = await parse.text(this);

// either
var body = await parse(this);
```

# License

[MIT](LICENSE.txt)

---
_Source: https://npm.io/package/co-body · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
