Simplified HTTP Request for Node.js

1.2.3 · abandoned · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to perform a simple GET request, a POST request with JSON data, and download a file using the callback-based API of `ajax-request`.

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
  }
});

view raw JSON →