Machinepack HTTP
raw JSON →machinepack-http provides a simple, `jQuery.get()`-like interface for sending HTTP requests, scraping webpages, and streaming data within Node.js and Sails.js applications. Currently at version 9.0.0, this library aims for immediate productivity in common cloud API interactions. It differentiates itself by offering robust error negotiation, distinguishing between network failures (e.g., offline, server down) and deliberate server errors (non-2xx status codes), and providing low-level streaming access to responses. The package is maintained by the Sails.js core team and follows the node-machine specification, acting as a higher-level wrapper around an underlying HTTP client. Its release cadence is tied to the Sails.js ecosystem, focusing on stability and integration.
Common errors
error TypeError: Http.get is not a function ↓
const Http = require('machinepack-http'); is present and that you are calling methods like Http.get() on the required object. error Error: connect ECONNREFUSED ↓
error Error: getaddrinfo ENOTFOUND ↓
error Error: Unknown machine: [machineName] ↓
node-machine.org documentation for machinepack-http to see the list of available machines and their correct names. Ensure correct casing. Warnings
breaking machinepack-http v9.0.0 explicitly depends on the `request` package, which has been deprecated and is no longer maintained since 2019. The `request` package contains known security vulnerabilities and does not receive security updates. Relying on `machinepack-http` introduces these vulnerabilities into your application. ↓
gotcha This package is built on the 'machinepack' specification, which uses a declarative, `.exec()`-based API. This differs significantly from Promise-based or callback-based HTTP clients. All 'machines' (like `get`, `post`, `send`) must be explicitly executed via a `.exec()` call, which then takes an object of exit handlers (e.g., `success`, `error`, `non2xx`). ↓
gotcha machinepack-http distinguishes between network errors (e.g., DNS resolution failure, connection refused) caught by the `error` exit, and non-2xx HTTP responses (e.g., 404, 500) caught by the `non2xx` exit. Developers commonly conflate these, leading to incomplete error handling. ↓
deprecated The underlying `request` library, which `machinepack-http` relies on, has been formally deprecated. While `machinepack-http` itself is maintained by the Sails.js core team, its fundamental dependency's deprecation indicates a significant risk and lack of future-proofing for this package. ↓
Install
npm install machinepack-http yarn add machinepack-http pnpm add machinepack-http Imports
- Http wrong
import Http from 'machinepack-http';correctconst Http = require('machinepack-http'); - Http.send wrong
import { send } from 'machinepack-http'; send(/* ... */);correctconst Http = require('machinepack-http'); Http.send(/* ... */).exec(/* ... */); - Http.get wrong
require('machinepack-http').get(/* ... */);correctconst Http = require('machinepack-http'); Http.get(/* ... */).exec(/* ... */);
Quickstart
const Http = require('machinepack-http');
const TARGET_URL = process.env.TARGET_URL ?? 'https://example.com/api/data';
Http.get({
url: TARGET_URL,
data: { userId: 123 },
timeout: 5000,
headers: { 'User-Agent': 'Machinepack-HTTP-Example' }
})
.exec({
// Handle network-level errors (e.g., server down, DNS issues)
error: function (err) {
console.error('Network or unexpected error:', err.message);
// console.error(err); // Uncomment for full error stack
},
// Handle HTTP errors (non-2xx status codes) detected by the server
non2xx: function (response) {
console.warn(`Server responded with non-2xx status: ${response.statusCode}`);
console.warn('Response body:', response.body);
},
// Handle successful 2xx responses
success: function (response) {
console.log('Successfully fetched data!');
console.log(`Status Code: ${response.statusCode}`);
console.log('Response body:', JSON.parse(response.body));
}
});