bhttp HTTP Client
bhttp is a Node.js HTTP client library designed to provide a sane and intuitive API, offering Streams2 support, automatic payload detection (URL-encoded, multipart/form-data, stream, Buffer, JSON), and robust session management with automatic cookie handling. It differentiates itself from alternatives like the core `http` module, `request`, `needle`, and `hyperquest` by addressing common issues such as low-level API complexity, agent pool blocking, and poor documentation. Key features include easy-to-use promises or nodebacks, multipart/form-data support with Streams2, and progress events for uploads and downloads. The package is currently at version 1.2.8, but its npm and GitHub activity indicate it has been abandoned for several years, making new releases unlikely.
Common errors
-
Error: write EPROTO
cause Attempting an HTTPS request without explicitly configuring a `https.Agent` that handles SSL/TLS. bhttp ignores SSL-related options by default.fixProvide a `https.Agent` to `bhttp`'s options. For example: `const https = require('https'); const agent = new https.Agent({ rejectUnauthorized: true }); bhttp.get('https://example.com', { agent: agent });` -
TypeError: bhttp.get is not a function
cause Incorrect import of the bhttp module or trying to call methods on an undefined `bhttp` object. This usually happens if `require('bhttp')` fails or if the module structure is misunderstood (e.g., expecting named exports).fixEnsure `bhttp` is correctly imported as a CommonJS module: `const bhttp = require('bhttp');`. Verify that `bhttp` is installed (`npm install bhttp`) and the path is correct.
Warnings
- breaking bhttp does not use a HTTPS-capable agent by default, and all SSL-related options are ignored. This is a critical security vulnerability for production applications using HTTPS without explicit configuration.
- gotcha The `bhttp` package appears to be abandoned, with the last npm publish and GitHub activity dating back over 8 years. This means there are no new features, bug fixes, or security updates, making it unsuitable for modern production environments due to potential unpatched vulnerabilities and compatibility issues with newer Node.js versions.
- gotcha While bhttp supports Streams2, its age suggests potential incompatibility or suboptimal performance with newer Node.js stream APIs (e.g., Web Streams, `async` iterators).
Install
-
npm install bhttp -
yarn add bhttp -
pnpm add bhttp
Imports
- bhttp
const bhttp = require('bhttp'); - bhttp.get
const bhttp = require('bhttp'); bhttp.get('http://example.com'); - bhttp.post
const bhttp = require('bhttp'); bhttp.post('http://example.com/api', { data: 'payload' });
Quickstart
const Promise = require("bluebird");
const bhttp = require("bhttp");
const https = require('https');
// Workaround for bhttp's lack of default HTTPS agent
const httpsAgent = new https.Agent({ rejectUnauthorized: false }); // WARNING: Do not use rejectUnauthorized: false in production
Promise.try(function() {
// Using the custom HTTPS agent for a secure request (example with a test endpoint)
return bhttp.get("https://icanhazip.com/", { agent: httpsAgent });
}).then(function(response) {
console.log("Your IP is:", response.body.toString());
}).catch(function(err) {
console.error("Error fetching IP:", err.message);
});
// Example using nodebacks for a simple HTTP request (no HTTPS agent needed here)
bhttp.get("http://example.com/data", {}, function(err, response) {
if (err) {
console.error("Error with nodeback request:", err.message);
return;
}
console.log("Nodeback response status:", response.statusCode);
});