Simplified JSON HTTP Client

0.6.5 · abandoned · verified Wed Apr 22

request-json is a Node.js HTTP client specifically designed to streamline interactions with JSON APIs. It acts as a wrapper around the now-deprecated `request` library, simplifying common tasks like sending and receiving JSON payloads, handling basic authentication, and uploading/downloading files. Currently at version 0.6.5, the package has seen no updates for several years, effectively rendering it unmaintained. Its core differentiator was abstracting away some complexities of the underlying `request` library, offering a more fluent API for JSON-centric operations and basic promise support via `.then()`. Due to its fundamental dependency on the unmaintained `request` package, `request-json` should be considered unstable and potentially insecure for new projects, with no ongoing release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the request-json client and perform various HTTP methods (POST, GET, PUT, DELETE, PATCH) to a mock API endpoint, including basic authentication and custom headers. It showcases both callback-based and promise-based approaches.

const request = require('request-json');
const client = request.createClient('http://localhost:8888/');

console.log('--- Simulating API calls to http://localhost:8888/ ---');

// POST request
const postData = {
  title: 'my title',
  content: 'my content'
};
client.post('posts/', postData, function(err, res, body) {
  if (err) return console.error('POST Error:', err);
  console.log(`POST /posts/ Status: ${res.statusCode}`);
  // console.log('POST Body:', body); // In a real scenario, body would contain the API's response
});

// GET request
client.get('posts/', function(err, res, body) {
  if (err) return console.error('GET Error:', err);
  console.log(`GET /posts/ Status: ${res.statusCode}`);
  // console.log('GET Body (first title):', body?.rows?.[0]?.title); // Assuming a structure, this would be undefined without a real server
});

// PUT request
const putData = {
  title: 'my new title'
};
client.put('posts/123/', putData, function(err, res, body) {
  if (err) return console.error('PUT Error:', err);
  console.log(`PUT /posts/123/ Status: ${res.statusCode}`);
});

// DELETE request
client.del('posts/123/', function(err, res, body) {
  if (err) return console.error('DEL Error:', err);
  console.log(`DEL /posts/123/ Status: ${res.statusCode}`);
});

// PATCH request with Promises
const patchData = {
  title: 'my patched title'
};
client.patch('posts/123/', patchData)
  .then(function(result) {
    console.log(`PATCH /posts/123/ Status: ${result.res.statusCode}`);
    // console.log('PATCH Body:', result.body);
  })
  .catch(err => {
    console.error('PATCH Error:', err);
  });

// Basic Authentication example
client.setBasicAuth('john', 'secret');
client.get('private/posts/', function(err, res, body) {
  if (err) return console.error('Auth GET Error:', err);
  console.log(`Authenticated GET /private/posts/ Status: ${res.statusCode}`);
});

// Headers manipulation
client.headers['X-Custom-Header'] = 'MyValue';
console.log('Client configured with X-Custom-Header.');

view raw JSON →