http-service

raw JSON →
1.1.3 verified Fri May 01 auth: no javascript maintenance

A simple Node.js module wrapping Node's native http/https module, targeting JSON data exchange between servers. Version 1.1.3 provides GET, POST, PUT, DELETE requests with both callback and Promise support. It allows base URL initialization and can be subclassed for custom service APIs. Limited documentation and low maintenance activity; no significant updates since 2018. Suitable for simple JSON-based server-to-server communication but lacks advanced features like interceptors, retries, or streaming support found in Axios or Got.

error Cannot find module 'http-service'
cause Package not installed or not in node_modules.
fix
Run 'npm install http-service' in your project directory.
error TypeError: service.get is not a function
cause Trying to call method on the class itself instead of an instance.
fix
Create an instance: const service = new HttpService(); then call service.get().
error Error: getaddrinfo ENOTFOUND
cause Invalid or unreachable hostname passed to init.
fix
Ensure the URL is correct and the server is reachable. Use an IP or valid domain.
gotcha The `init` method does not validate the URL or handle protocol errors gracefully; invalid URLs may cause unhandled exceptions.
fix Always validate the URL before calling init, and use try-catch around instantiation.
deprecated Callbacks are the primary pattern but Promise support exists; mixing both in the same codebase can lead to confusion.
fix Choose one pattern (prefer callbacks for consistency) and stick to it.
gotcha The library does not support HTTPS strict SSL by default; self-signed certificates may cause failures unless NODE_TLS_REJECT_UNAUTHORIZED is set.
fix Set environment variable NODE_TLS_REJECT_UNAUTHORIZED=0 (not recommended for production) or use a custom agent.
gotcha Query parameters are passed as an object, but the library does not deeply serialize nested objects; only first-level keys are sent.
fix Serialize nested query parameters manually using a library like qs or querystring.
npm install http-service
yarn add http-service
pnpm add http-service

Shows basic usage: creating an instance, initializing with a base URL, and performing GET, POST, and DELETE requests with callbacks and Promises.

const HttpService = require('http-service');

const service = new HttpService();
service.init(process.env.API_URL || 'http://localhost:3000');

// GET with callback
service.get('/data', {limit: 10}, (err, data) => {
  if (err) console.error('Error:', err);
  else console.log('Data:', data);
});

// POST with Promise (no callback)
service.post('/data', {}, {name: 'test'})
  .then(data => console.log('Created:', data))
  .catch(err => console.error('Error:', err));

// DELETE
service.delete('/data/1', {}, (err, result) => {
  if (err) console.error('Delete failed:', err);
  else console.log('Deleted:', result);
});