Bent is a small, functional, and async/await-based HTTP client designed for both Node.js and browsers. Currently stable at version 7.3.12, it provides a fluent API for making HTTP requests with a focus on type-driven configuration. Key differentiators include its remarkably small bundle size (especially for browsers, where it's built on `fetch` with no external dependencies), flexible option parsing (inferring method, format, status codes, and base URL from argument types), and explicit error handling for unacceptable response statuses. While mature, its release cadence is not high, indicating stability over rapid feature churn. It differs from alternatives by prioritizing a functional, curried API over object-oriented design and by requiring explicit declaration of accepted status codes, which helps prevent unexpected success conditions.
npm install bentVerified import paths — ran on the pinned version, not inferred.
Demonstrates creating different `bent` instances for GET (JSON) and POST (JSON) requests, and fetching binary data as a buffer, with error handling.
Ensure all expected successful status codes, including `200`, are passed to the `bent` factory function, e.g., `bent(200, 201)`.
Use `const postForm = bent('POST', { 'Content-Type': 'application/x-www-form-urlencoded' }); await postForm(url, new URLSearchParams({ key: 'value' }).toString());`Always check the environment and use the appropriate binary type. In Node.js, `Buffer.from('data')`; in browser, `new TextEncoder().encode('data')` for `ArrayBufferView` or `new ArrayBuffer(...)`.In Node.js ESM projects, if a `bent` ESM build isn't available or preferred, use dynamic `import('bent').then(m => m.default || m)` or consider a CommonJS wrapper. For browser, bundling typically handles ESM imports correctly.Change the import to `const bent = require('bent');` or use a dynamic import `const bent = await import('bent');` and then access `bent.default || bent` for the actual function.Modify your `bent` factory call to include all expected successful status codes, e.g., `bent(200, 201, 404)` if 404 is an expected, handled case.
Ensure the server is sending `Content-Type: application/json` and valid JSON. If the content type varies, create `bent` instances that don't specify a response format (`bent()`) and use `response.json()` conditionally, or `bent('string')` for text responses.If you need a specific format (like `buffer` or `json`), configure your `bent` instance with that format (e.g., `const getBuffer = bent('buffer');`). If not, handle the raw Node.js stream or use the `.arrayBuffer()`/`.text()`/`.json()` methods provided on the response object (matching Fetch API) after checking its type or status.No dependency data recorded yet.