Simplified HTTP Request for Node.js
ajax-request is a basic, callback-based HTTP client library designed for Node.js, offering functionalities for sending GET and POST requests, as well as direct file downloads. Currently at version 1.2.3, this package appears to have an inactive or ceased release cadence, with its last update occurring approximately 8 years ago. Key differentiators include its straightforward API for common HTTP operations and built-in file downloading, which was more unique at the time of its active development. However, it significantly lags behind modern HTTP clients, lacking support for Promises/async-await and ES Modules, and does not ship with TypeScript definitions. The `base64` utility method has been deprecated and migrated to a separate package, signaling a lack of ongoing maintenance for this core library.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module [path] not supported
cause Attempting to `import` the `ajax-request` package in an ES Module context.fixChange `import request from 'ajax-request';` to `const request = require('ajax-request');` and ensure your project is configured for CommonJS or uses dynamic `import()` if mixed modules are necessary. -
TypeError: callback is not a function
cause The request method (or its variants) was called without providing a callback function as the last argument.fixEnsure that a function is passed as the `callback` argument to `request()`, `request.post()`, or `request.download()` methods. -
Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE
cause The Node.js environment failed to verify the SSL certificate of the target server, often due to an outdated root certificate store or a self-signed certificate.fixUpdate Node.js to a recent version. If the server uses a self-signed certificate in a controlled environment, you might temporarily set `NODE_TLS_REJECT_UNAUTHORIZED='0'` (NOT for production) or add the certificate to your trusted store. The best fix is to migrate to a modern, maintained HTTP client library.
Warnings
- gotcha The `ajax-request` package is significantly outdated (v1.2.3, last published ~8 years ago) and appears to be unmaintained. It lacks modern features like Promise-based APIs or `async/await` support, relying solely on callbacks.
- breaking The package is CommonJS-only and does not support ES Modules. Attempting to use `import` syntax will result in a runtime `ERR_REQUIRE_ESM` error.
- deprecated The `request.base64` method has been explicitly deprecated and moved to a separate package. Its functionality is no longer part of `ajax-request`.
- gotcha Due to its age, `ajax-request` might not correctly handle modern TLS/SSL certificates, leading to `UNABLE_TO_VERIFY_LEAF_SIGNATURE` or similar errors when connecting to contemporary HTTPS endpoints. It also lacks explicit security updates for potential vulnerabilities.
- gotcha The package does not ship with TypeScript type definitions, leading to poor developer experience and lack of type safety in TypeScript projects.
Install
-
npm install ajax-request -
yarn add ajax-request -
pnpm add ajax-request
Imports
- request
import request from 'ajax-request';
const request = require('ajax-request'); - request.post
import { post } from 'ajax-request';const request = require('ajax-request'); request.post(options, callback); - request.download
import { download } from 'ajax-request';const request = require('ajax-request'); request.download(options, callback);
Quickstart
const request = require('ajax-request');
// Example 1: Basic GET request
request('https://jsonplaceholder.typicode.com/posts/1', function(err, res, body) {
if (err) {
console.error('GET Error:', err);
return;
}
console.log('GET Response (Status Code):', res.statusCode);
console.log('GET Response (Body):', JSON.parse(body));
});
// Example 2: POST request with data
request({
url: 'https://jsonplaceholder.typicode.com/posts',
method: 'POST',
data: {
title: 'foo',
body: 'bar',
userId: 1
},
json: true // Automatically parse response body as JSON
}, function(err, res, body) {
if (err) {
console.error('POST Error:', err);
return;
}
console.log('POST Response (Status Code):', res.statusCode);
console.log('POST Response (Body):', body);
});
// Example 3: Download a file (replace with a real, small file URL for testing)
// NOTE: This will attempt to save a file to the current working directory.
const fs = require('fs');
const tempFilePath = './downloaded-image.png';
request.download({
url: 'https://via.placeholder.com/150/FF0000/FFFFFF?text=Test',
destPath: tempFilePath
}, function(err, res, body, destpath) {
if (err) {
console.error('Download Error:', err);
return;
}
console.log(`File downloaded to: ${destpath}`);
// Verify file existence (optional)
if (fs.existsSync(tempFilePath)) {
console.log('Downloaded file exists!');
fs.unlinkSync(tempFilePath); // Clean up
}
});