Tiny JSON HTTP Client
tiny-json-http is a minimalist HTTP client designed for making GET, POST, PUT, PATCH, and DELETE requests, primarily handling JSON payloads. Currently stable at version 7.5.1, it emphasizes a small footprint with zero external dependencies, making it particularly well-suited for constrained environments like AWS Lambda functions. The library offers a dual API, supporting both Node.js-style errback callbacks and Promises, which enables modern `async/await` usage. Its core differentiators include automatically assuming buffered JSON responses, a system-symmetric API, and a focus on simplicity over extensive configuration, making it a straightforward choice for basic HTTP interactions without the overhead of more feature-rich clients. The project maintains a steady release cadence for bug fixes and minor improvements, with major versions introducing breaking changes as needed.
Common errors
-
TypeError: tiny.get is not a function
cause Attempting to use `import` syntax (e.g., `import tiny from 'tiny-json-http'`) in a pure CommonJS environment, or incorrect destructuring of the required module.fixFor CommonJS, use `const tiny = require('tiny-json-http')`. If you must use `import`, ensure your bundler or Node.js environment is configured to handle CommonJS module imports correctly. -
SyntaxError: Unexpected token < in JSON at position 0 (or similar JSON parsing error)
cause The server responded with an HTML page (e.g., an error page), plain text, or an empty body, but tiny-json-http automatically attempted to parse it as JSON.fixIf the response is not expected to be JSON, set the `buffer: true` option to receive the raw response body. You can then check its content type or parse it manually if desired. -
getaddrinfo ENOTFOUND example.com (or similar network error like ECONNREFUSED)
cause The specified `url` is incorrect, the domain could not be resolved (DNS error), or the target server is unreachable or refused the connection.fixVerify the URL for typos, check your network connectivity, and ensure the target server is online and accessible from where your application is running. Implement retry logic for transient network issues. -
UnhandledPromiseRejectionWarning: [error message]
cause A promise returned by tiny-json-http was rejected due to an error (e.g., network failure, server error response), but there was no `.catch()` handler or `try...catch` block to handle the error.fixAlways add a `.catch()` method to promise chains (e.g., `tiny.get(...).then(...).catch(err => ...)`) or wrap `await` calls in a `try...catch` block to gracefully handle errors.
Warnings
- gotcha tiny-json-http automatically attempts to parse responses as JSON. If the server returns a non-JSON body (e.g., HTML error pages, plain text, or empty responses), this will result in a JSON parsing error.
- gotcha The `del` (DELETE) method accepts a `data` object, which is sent as the request body. Some REST APIs expect DELETE parameters in the URL query string rather than the request body, which can lead to unexpected behavior or API rejection.
- gotcha While tiny-json-http supports both callbacks and Promises, failing to handle errors in either pattern can lead to unhandled promise rejections or uncaught exceptions, potentially crashing your application.
- gotcha The library does not expose options for configuring request timeouts directly in the public API excerpt provided. This means requests could hang indefinitely in cases of unresponsive servers, affecting application stability.
Install
-
npm install tiny-json-http -
yarn add tiny-json-http -
pnpm add tiny-json-http
Imports
- tiny
import tiny from 'tiny-json-http'
const tiny = require('tiny-json-http') - get
import { get } from 'tiny-json-http'const { get } = require('tiny-json-http') - post
import { post } from 'tiny-json-http'const { post } = require('tiny-json-http')
Quickstart
const tiny = require('tiny-json-http');
const url = 'https://jsonplaceholder.typicode.com/posts/1';
(async function _iife() {
try {
const result = await tiny.get({ url });
console.log('Successfully fetched data:');
console.log('Headers:', result.headers);
console.log('Body:', result.body);
} catch (err) {
console.error('Error fetching data:', err.message);
if (err.statusCode) {
console.error('Status Code:', err.statusCode);
console.error('Error Body:', err.body); // Server error body if available
}
}
})();