http-response-object is a minimalistic JavaScript library designed to encapsulate HTTP response data into a simple, consistent object. It provides a standardized structure for common response properties such as `statusCode`, `headers`, `body`, and `url`, which simplifies handling and passing HTTP responses across different application layers. The current stable version is 3.0.2. While the project maintains a focused scope, it receives updates to ensure compatibility and address specific use cases. Key differentiators include its lightweight design, explicit support for both Node.js `Buffer` and `String` for the response body, and the inclusion of TypeScript types, which enhance developer experience through strong type checking. It is frequently employed in contexts requiring a normalized representation of HTTP responses, such as in HTTP client abstractions, proxy implementations, or server-side rendering logic, offering a predictable interface for response data.
npm install http-response-objectVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create `Response` objects for both successful and error scenarios, access their properties, and utilize the `getBody()` method, including handling its error-throwing behavior for non-2xx status codes. It also highlights the automatic lowercasing of header keys.
Always access headers using their lowercase form (e.g., `response.headers['content-type']`) or normalize keys before comparison.
Always wrap calls to `getBody()` in a `try...catch` block or ensure `statusCode` is 2xx before calling it. The thrown error object will contain `statusCode`, `headers`, `body`, and `url` properties for inspection.
If expecting a string, call `.toString()` on `response.body`. If expecting a Buffer, ensure inputs are always Buffers or convert strings to Buffers explicitly.
Implement a `try...catch` block around `response.getBody()` or explicitly check `response.statusCode` to be within the 200-299 range before calling `getBody()`.
Always access headers using their lowercase names (e.g., `response.headers['content-type']` instead of `response.headers['Content-Type']`). Verify the header exists before accessing if it's optional.
No dependency data recorded yet.