Simplified JSON HTTP Client
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
-
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
cause This warning appears because `request-json` depends on the `request` package, which is no longer maintained.fixWhile this specific warning cannot be 'fixed' within `request-json` without updating its core dependency, it signals an urgent need to migrate away from `request-json` to a modern HTTP client library for security and stability reasons. -
TypeError: request.createClient is not a function
cause Attempting to use `new` keyword with `createClient()` or calling `createClient` before requiring `request-json`.fixEnsure `const request = require('request-json');` is correctly placed and `createClient()` is called directly on the imported `request` object: `const client = request.createClient('http://localhost:8888/');`. -
UnhandledPromiseRejectionWarning: Unhandled promise rejection.
cause A promise-returning method (e.g., `client.get().then(...)`) encountered an error, but no `.catch()` handler was provided.fixAlways chain a `.catch(err => { console.error(err); })` to any promise-returning `request-json` method to handle potential errors.
Warnings
- breaking The underlying `request` library, which `request-json` relies on, was fully deprecated on February 11, 2020. This means `request-json` is built on an unmaintained and potentially insecure foundation, lacking future updates, bug fixes, or security patches.
- deprecated The `newClient()` method is deprecated. While still functional, it will emit a deprecation warning.
- gotcha Error handling typically relies on Node.js-style callbacks (`(err, res, body) => {}`) where the first argument is an error. For promise-based calls, `.catch()` should be used.
- gotcha The package uses CommonJS `require()` syntax exclusively. It does not provide native ES module exports, which is the standard for modern JavaScript projects.
- gotcha File upload methods (`sendFile`) have specific key naming conventions (`'file'` for single files, `'file0', 'file1', ...` for arrays) which might not align with all API expectations, requiring server-side adaptation or careful client-side data preparation.
Install
-
npm install request-json -
yarn add request-json -
pnpm add request-json
Imports
- request
import request from 'request-json'
const request = require('request-json'); - createClient
const client = new request.newClient('http://localhost:8888/');const client = request.createClient('http://localhost:8888/'); - post
client.post('posts/', data, (err, res, body) => { /* ... */ });
Quickstart
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.');