restler-base

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

restler-base is a fork of the unmaintained restler HTTP client library for Node.js, version 3.4.6. It incorporates pending pull requests and fixes from the original project, serving as the foundation for restler-promise. The library provides an event-based API for making HTTP requests with automatic serialization of post data and query strings, transparent handling of SSL, redirects, gzip/deflate, and basic auth, plus automatic deserialization of XML, JSON, and YAML responses. It supports multipart file uploads and custom deserializers. Compared to modern alternatives like axios or node-fetch, restler-base is older and uses an event-driven pattern rather than promises, though it can be wrapped with restler-promise. Release cadence is low; last release was 3.4.6. Key differentiator: it is a maintained fork of restler, preserving its specific API for legacy projects.

error Cannot find module 'restler-base'
cause Package not installed or imported from wrong package name.
fix
Run 'npm install restler-base' and ensure import is from 'restler-base' not 'restler'.
error TypeError: Cannot read property 'on' of undefined
cause Forgetting to call a method like get() or post() returns a request object, but if URL is invalid or options cause immediate error, may return undefined.
fix
Check URL and options, ensure function returns a RestRequest object before chaining .on()
error Error: connect ECONNREFUSED 127.0.0.1:80
cause No server listening on the target host/port (e.g., localhost test server not started).
fix
Ensure target server is running, or use a valid URL.
error Warning: Possible EventEmitter memory leak detected. 11 complete listeners added.
cause Adding many listeners on the same request object without removing or using once.
fix
Use .once() instead of .on() if listeners should fire only once, or manage listener removal with .removeListener().
gotcha The 'complete' event fires even on errors, and the first argument is an Error instance on failure. Always check if result instanceof Error.
fix Always check 'result instanceof Error' in 'complete' event handler before using result.
deprecated Library uses a callback/event-driven pattern; newer projects should consider axios or node-fetch with promise support.
fix Consider using the restler-promise wrapper for promise-based API, or migrate to axios.
gotcha Automatic XML/YAML deserialization requires optional dependencies (xml2js, yamljs). Without them, responses will be strings.
fix Install optional dependencies: npm install xml2js yamljs
gotcha The 'timeout' option is in milliseconds; if not set, requests may hang indefinitely.
fix Always set timeout option to a reasonable value (e.g., 10000).
gotcha When using 'multipart: true', you must provide a 'data' object containing file properties with 'file' and 'name' sub-fields.
fix Format multipart data as { file: { file: '/path/to/file', name: 'fieldname' } }.
npm install restler-base
yarn add restler-base
pnpm add restler-base

Demonstrates basic HTTP GET and POST using the event-based API with error handling via 'complete' and 'success'/'fail' events.

import restler from 'restler-base';

restler.get('https://api.example.com/data', {
  headers: { 'Accept': 'application/json' },
  timeout: 5000
}).on('complete', (result, response) => {
  if (result instanceof Error) {
    console.error('Error:', result.message);
  } else {
    console.log('Data:', result);
  }
});

restler.post('https://api.example.com/submit', {
  data: { name: 'test', value: 123 },
  headers: { 'Content-Type': 'application/json' }
}).on('success', (data, response) => {
  console.log('Posted successfully:', data);
}).on('fail', (data, response) => {
  console.error('Failed with status:', response.statusCode);
});