{"id":13527668,"url":"https://github.com/koajs/bodyparser","last_synced_at":"2025-05-14T04:07:28.638Z","repository":{"id":14211396,"uuid":"16918137","full_name":"koajs/bodyparser","owner":"koajs","description":"Koa body parsing middleware","archived":false,"fork":false,"pushed_at":"2024-06-14T05:21:01.000Z","size":119,"stargazers_count":1316,"open_issues_count":3,"forks_count":119,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-05-08T01:34:06.429Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/koajs.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-02-17T16:02:53.000Z","updated_at":"2025-04-26T09:28:28.000Z","dependencies_parsed_at":"2024-01-12T23:42:53.845Z","dependency_job_id":"9fcc8ad6-18ec-48ee-b24b-eee4eb045dca","html_url":"https://github.com/koajs/bodyparser","commit_stats":{"total_commits":97,"total_committers":29,"mean_commits":"3.3448275862068964","dds":0.4639175257731959,"last_synced_commit":"031348a2d469dd288dc21cd10d8de280d9936f2c"},"previous_names":["koajs/body-parser"],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fbodyparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fbodyparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fbodyparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fbodyparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koajs","download_url":"https://codeload.github.com/koajs/bodyparser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253728952,"owners_count":21954529,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:01:56.341Z","updated_at":"2025-05-14T04:07:28.605Z","avatar_url":"https://github.com/koajs.png","language":"TypeScript","readme":"# [**@koa/bodyparser**](https://github.com/koajs/bodyparser)\n\n[![NPM version][npm-image]][npm-url]\n![build status][github-action-image]\n[![Coveralls][coveralls-image]][coveralls-url]\n[![node version][node-image]][node-url]\n\n[npm-image]: https://img.shields.io/npm/v/@koa/bodyparser.svg?style=flat-square\n[npm-url]: https://www.npmjs.com/package/@koa/bodyparser\n[github-action-image]: https://github.com/koajs/bodyparser/actions/workflows/ci.yml/badge.svg?style=flat-square\n[coveralls-image]: https://img.shields.io/coveralls/koajs/bodyparser.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/koajs/bodyparser?branch=master\n[node-image]: https://img.shields.io/badge/node.js-%3E=_14-green.svg?style=flat-square\n[node-url]: http://nodejs.org/download/\n\nKoa body parsing middleware, based on [co-body](https://github.com/tj/co-body). support `json`, `form` and `text` type body.\n\nParse incoming request bodies in a middleware before your handlers, available under the `ctx.request.body` property.\n\n\u003e ⚠ Notice: **This module doesn't support parsing multipart format data**, please use [`@koa/multer`](https://github.com/koajs/multer) to parse multipart format data.\n\n## Install\n\n[![NPM](https://nodei.co/npm/@koa/bodyparser.png?downloads=true)](https://nodei.co/npm/@koa/bodyparser)\n\n```bash\n$ npm i @koa/bodyparser --save\n```\n\n## Usage\n\n```js\nconst Koa = require(\"koa\");\nconst { bodyParser } = require(\"@koa/bodyparser\");\n\nconst app = new Koa();\napp.use(bodyParser());\n\napp.use((ctx) =\u003e {\n  // the parsed body will store in ctx.request.body\n  // if nothing was parsed, body will be an empty object {}\n  ctx.body = ctx.request.body;\n});\n```\n\n## Options\n\n- **patchNode**: patch request body to Node's `ctx.req`, default is `false`.\n- **enableTypes**: parser will only parse when request type hits enableTypes, support `json/form/text/xml`, default is `['json', 'form']`.\n- **encoding**: requested encoding. Default is `utf-8` by `co-body`.\n- **formLimit**: limit of the `urlencoded` body. If the body ends up being larger than this limit, a 413 error code is returned. Default is `56kb`.\n- **jsonLimit**: limit of the `json` body. Default is `1mb`.\n- **textLimit**: limit of the `text` body. Default is `1mb`.\n- **xmlLimit**: limit of the `xml` body. Default is `1mb`.\n- **jsonStrict**: when set to true, JSON parser will only accept arrays and objects. Default is `true`. See [strict mode](https://github.com/cojs/co-body#options) in `co-body`. In strict mode, `ctx.request.body` will always be an object(or array), this avoid lots of type judging. But text body will always return string type.\n- **detectJSON**: custom json request detect function. Default is `null`.\n\n  ```js\n  app.use(\n    bodyParser({\n      detectJSON(ctx) {\n        return /\\.json$/i.test(ctx.path);\n      },\n    })\n  );\n  ```\n\n- **extendTypes**: support extend types:\n\n  ```js\n  app.use(\n    bodyParser({\n      extendTypes: {\n        // will parse application/x-javascript type body as a JSON string\n        json: [\"application/x-javascript\"],\n      },\n    })\n  );\n  ```\n\n- **onError**: support custom error handle, if `koa-bodyparser` throw an error, you can customize the response like:\n\n  ```js\n  app.use(\n    bodyParser({\n      onError(err, ctx) {\n        ctx.throw(422, \"body parse error\");\n      },\n    })\n  );\n  ```\n\n- **enableRawChecking**: support the already parsed body on the raw request by override and prioritize the parsed value over the sended payload. (default is `false`)\n\n- **parsedMethods**: declares the HTTP methods where bodies will be parsed, default `['POST', 'PUT', 'PATCH']`.\n\n- **disableBodyParser**: you can dynamic disable body parser by set `ctx.disableBodyParser = true`.\n\n  ```js\n  app.use((ctx, next) =\u003e {\n    if (ctx.path === \"/disable\") ctx.disableBodyParser = true;\n    return next();\n  });\n  app.use(bodyParser());\n  ```\n\n## Raw Body\n\nYou can access raw request body by `ctx.request.rawBody` after `koa-bodyparser` when:\n\n1. `koa-bodyparser` parsed the request body.\n2. `ctx.request.rawBody` is not present before `koa-bodyparser`.\n\n## Koa v1.x.x Support\n\nTo use `koa-bodyparser` with koa@1.x.x, please use [bodyparser 2.x](https://github.com/koajs/bodyparser/tree/2.x).\n\n```bash\n$ npm install koa-bodyparser@2 --save\n```\n\nusage\n\n```js\nconst Koa = require(\"koa\");\nconst bodyParser = require(\"@koa/bodyparser\");\n\nconst app = new Koa();\napp.use(bodyParser());\n\napp.use((ctx) =\u003e {\n  // the parsed body will store in ctx.request.body\n  // if nothing was parsed, body will be an empty object {}\n  ctx.body = ctx.request.body;\n});\n```\n\n## Licences\n\n[MIT](LICENSE)\n","funding_links":[],"categories":["TypeScript","JavaScript","Middleware","Node.js, koa"],"sub_categories":["Utilites"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fbodyparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoajs%2Fbodyparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fbodyparser/lists"}