{"id":18024007,"url":"https://github.com/lete114/body-data","last_synced_at":"2025-07-30T14:41:50.633Z","repository":{"id":74790119,"uuid":"438952291","full_name":"lete114/Body-Data","owner":"lete114","description":"A lightweight Node.js utility to extract query parameters and request body data from `IncomingMessage`. Supports common `Content-Type` formats and safe fallbacks.","archived":false,"fork":false,"pushed_at":"2025-07-02T14:03:29.000Z","size":131,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-02T14:03:30.133Z","etag":null,"topics":["body","data","get","http-server","params","parse","post","request"],"latest_commit_sha":null,"homepage":"https://body-data.vercel.app","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/lete114.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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,"zenodo":null}},"created_at":"2021-12-16T10:40:40.000Z","updated_at":"2025-07-02T13:50:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"f9c8c021-8be9-4117-b50f-7acd70f1ed94","html_url":"https://github.com/lete114/Body-Data","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/lete114/Body-Data","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lete114%2FBody-Data","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lete114%2FBody-Data/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lete114%2FBody-Data/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lete114%2FBody-Data/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lete114","download_url":"https://codeload.github.com/lete114/Body-Data/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lete114%2FBody-Data/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267884026,"owners_count":24160237,"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","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["body","data","get","http-server","params","parse","post","request"],"created_at":"2024-10-30T07:11:38.703Z","updated_at":"2025-07-30T14:41:50.597Z","avatar_url":"https://github.com/lete114.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 📦 body-data\n\n[![visitors][visitors-src]][visitors-href]\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![bundle][bundle-src]][bundle-href]\n[![JSDocs][jsdocs-src]][jsdocs-href]\n[![License][license-src]][license-href]\n\nA lightweight, zero-dependency utility for extracting query parameters and request body data from both Node.js IncomingMessage and Web standard Request objects. Fully supports Node.js (18+), Deno, Bun, Cloudflare Workers, and any modern JavaScript runtime. Type-safe, auto-adaptive, and supports all common Content-Type formats.\n\n## ✨ Features\n\n- ✅ Works in Node.js (18+), Deno, Bun, Cloudflare Workers, and browsers\n- ✅ Accepts both Node.js IncomingMessage and Web standard Request objects\n- ✅ Type-safe with full TypeScript generics support\n- ✅ Extract query parameters from `GET` requests\n- ✅ Parse request body from `POST` requests\n- ✅ Supports `application/json`, `x-www-form-urlencoded`, `text/plain`, `multipart/form-data`, and others\n- ✅ Safe fallback parsing\n- ✅ Zero dependencies\n- ✅ Configurable parsing options: encoding, content-type override, raw mode, and custom error handling\n\n## 📦 Installation\n\n```bash\nnpm install body-data\n# or\npnpm add body-data\n````\n\n---\n\n## 🚀 Usage Examples\n\n### 1. Using `bodyData`\n\n`bodyData` is a high-level utility that returns both `params` (query) and `body` data.\n\n```ts\nimport http from 'node:http'\nimport { bodyData } from 'body-data'\n\nhttp.createServer(async (req, res) =\u003e {\n  const data = await bodyData(req)\n\n  res.setHeader('Content-Type', 'application/json')\n  res.end(JSON.stringify(data))\n}).listen(3000)\n```\n\n**Request Example:**\n\n```bash\ncurl \"http://localhost:3000?name=lete\u0026age=18\" \\\n  -X POST \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"gender\":\"male\"}'\n```\n\n**Response:**\n\n```json\n{\n  \"params\": {\n    \"name\": \"lete\",\n    \"age\": \"18\"\n  },\n  \"body\": {\n    \"gender\": \"male\"\n  }\n}\n```\n\n---\n\n### 2. Using `getParams`\n\nUse `getParams` to only extract query parameters from the URL.\n\n```ts\nimport http from 'node:http'\nimport { getParams } from 'body-data'\n\nhttp.createServer((req, res) =\u003e {\n  const params = getParams(req)\n\n  res.setHeader('Content-Type', 'application/json')\n  res.end(JSON.stringify({ params }))\n}).listen(3000)\n```\n\n**Request Example:**\n\n```bash\ncurl \"http://localhost:3000?foo=bar\u0026count=10\"\n```\n\n**Response:**\n\n```json\n{\n  \"params\": {\n    \"foo\": \"bar\",\n    \"count\": \"10\"\n  }\n}\n```\n\n---\n\n### 3. Using `getBody`\n\nUse `getBody` to only extract the body from a `POST` request.\n\n```ts\nimport http from 'node:http'\nimport { getBody } from 'body-data'\n\nhttp.createServer(async (req, res) =\u003e {\n  const body = await getBody(req)\n\n  res.setHeader('Content-Type', 'application/json')\n  res.end(JSON.stringify({ body }))\n}).listen(3000)\n```\n\n**Request Example:**\n\n```bash\ncurl \"http://localhost:3000\" \\\n  -X POST \\\n  -H \"Content-Type: application/x-www-form-urlencoded\" \\\n  -d \"username=test\u0026password=1234\"\n```\n\n**Response:**\n\n```json\n{\n  \"body\": {\n    \"username\": \"test\",\n    \"password\": \"1234\"\n  }\n}\n```\n\n---\n\n## 📖 API Reference\n\n### `bodyData(req: IncomingMessage): Promise\u003c{ params, body }\u003e`\n\nReturns an object with:\n\n* `params`: Query parameters (from URL)\n* `body`: Parsed request body\n\n### `getParams(req: IncomingMessage): Record\u003cstring, any\u003e`\n\nParses the query string from the request URL.\n\n### `getBody(req: IncomingMessage): Promise\u003cRecord\u003cstring, any\u003e\u003e`\n\nParses the body of the request based on `Content-Type`. Supports:\n\n* `application/json`\n* `application/x-www-form-urlencoded`\n* `text/plain`\n* `multipart/form-data` (returns raw string)\n* Fallback: returns `{ raw: string }`\n\n#### Options (`IBodyOptions`):\n\n| Option            | Type                   | Description                                                 |\n| ----------------- | ---------------------- | ----------------------------------------------------------- |\n| `raw`             | `boolean`              | Return raw body string instead of parsing. Default: `false` |\n| `encoding`        | `BufferEncoding`       | Text encoding for reading the body. Default: `'utf-8'`      |\n| `contentType`     | `string`               | Force a specific `Content-Type` (overrides request headers) |\n| `backContentType` | `string`               | Fallback `Content-Type` when none is provided               |\n| `onError`         | `(err: Error) =\u003e void` | Custom error handler for parse or stream errors             |\n\n## ✅ Example with Custom Options\n\n```ts\nconst body = await getBody(req, {\n  raw: false,\n  encoding: 'utf-8',\n  contentType: 'application/json',\n  backContentType: 'text/plain',\n  onError: err =\u003e console.error('Body parse error:', err),\n})\n```\n\n## 🧪 Testing\n\n```bash\npnpm test\n```\n\n---\n\n## 📄 License\n\n[MIT](./LICENSE) License © [Lete114](https://github.com/lete114)\n\n\u003c!-- Badges --\u003e\n\n[visitors-src]: https://visitor-badge.imlete.cn/?id=github.Lete114/body-data\u0026labelColor=080f12\u0026color=1fa669\u0026type=pv\u0026style=flat\n[visitors-href]: https://github.com/Lete114/visitor-badge\n\n[npm-version-src]: https://img.shields.io/npm/v/body-data?style=flat\u0026colorA=080f12\u0026colorB=1fa669\n[npm-version-href]: https://npmjs.com/package/body-data\n\n[npm-downloads-src]: https://img.shields.io/npm/dm/body-data?style=flat\u0026colorA=080f12\u0026colorB=1fa669\n[npm-downloads-href]: https://npmjs.com/package/body-data\n\n[bundle-src]: https://img.shields.io/bundlephobia/minzip/body-data?style=flat\u0026colorA=080f12\u0026colorB=1fa669\u0026label=minzip\n[bundle-href]: https://bundlephobia.com/result?p=body-data\n\n[license-src]: https://img.shields.io/github/license/Lete114/body-data.svg?style=flat\u0026colorA=080f12\u0026colorB=1fa669\n[license-href]: https://github.com/Lete114/body-data/blob/main/LICENSE\n\n[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat\u0026colorA=080f12\u0026colorB=1fa669\n[jsdocs-href]: https://www.jsdocs.io/package/body-data\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flete114%2Fbody-data","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flete114%2Fbody-data","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flete114%2Fbody-data/lists"}