{"id":11449,"library":"node-rest-client","title":"Node.js REST Client","description":"node-rest-client is a versatile Node.js library for interacting with RESTful APIs, designed to simplify HTTP/HTTPS requests. It offers features such as transparent handling of HTTP/HTTPS connections, basic authentication, and support for common HTTP methods (GET, POST, PUT, DELETE, PATCH), with the ability to define custom methods. A key differentiator is its capability to register remote API operations as reusable client methods, streamlining code and promoting reuse. The library also manages dynamic path and query parameters, request headers, provides improved error handling, supports compressed responses (gzip, deflate), follows redirects (via `follow-redirects`), and allows for custom request serializers and response parsers (with JSON and XML included by default). The current stable version is 3.1.1. Release cadence is not strictly scheduled, but the project shows active development with recent major and minor updates.","status":"active","version":"3.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/aacerox/node-rest-client","tags":["javascript"],"install":[{"cmd":"npm install node-rest-client","lang":"bash","label":"npm"},{"cmd":"yarn add node-rest-client","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-rest-client","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used internally to provide automatic HTTP/HTTPS redirect following.","package":"follow-redirects","optional":false}],"imports":[{"note":"The library primarily uses CommonJS `require` syntax as demonstrated in its documentation. Direct ESM `import` statements may require specific Node.js configuration or dynamic imports for interoperability.","wrong":"import { Client } from 'node-rest-client';","symbol":"Client","correct":"const Client = require('node-rest-client').Client;"},{"note":"The `Client` class is a named export from the CommonJS module. Access it via the `.Client` property of the `require`'d module object.","wrong":"import nodeRestClient from 'node-rest-client';","symbol":"Client (full module)","correct":"const nodeRestClient = require('node-rest-client');\nconst Client = nodeRestClient.Client;"},{"note":"Registered methods are accessed through the `client.methods` object, not directly on the client instance.","wrong":"client.myMethod(function(data, response){ /* ... */ });","symbol":"Custom Method Registration","correct":"client.registerMethod('myMethod', 'http://example.com/api', 'GET');\nclient.methods.myMethod(function(data, response){ /* ... */ });"}],"quickstart":{"code":"const Client = require('node-rest-client').Client;\n\nconst client = new Client();\n\n// Example: Simple HTTP GET request\nclient.get(\"https://jsonplaceholder.typicode.com/posts/1\", function (data, response) {\n\t// parsed response body as js object\n\tconsole.log('GET Response Data:', data);\n\t// raw response\n\tconsole.log('GET Response Status:', response.statusCode);\n});\n\n// Example: HTTP POST request with JSON data and headers\nconst args = {\n\tdata: { title: 'foo', body: 'bar', userId: 1 },\n\theaders: { \"Content-Type\": \"application/json\" }\n};\n\nclient.post(\"https://jsonplaceholder.typicode.com/posts\", args, function (data, response) {\n\tconsole.log('POST Response Data:', data);\n\tconsole.log('POST Response Status:', response.statusCode);\n});","lang":"javascript","description":"This quickstart demonstrates how to install `node-rest-client` and perform both a simple HTTP GET request and an HTTP POST request with JSON data and headers to a public API."},"warnings":[{"fix":"Review your code for any direct calls to non-public `node-rest-client` methods. If such calls exist, either refactor to use documented public APIs or adjust to the new `$` prefixed names, noting this is an internal implementation detail and subject to change.","message":"With the introduction of v3.0.0, the library prefixed all internal methods with a `$` to prevent potential name clashes with custom registered methods. If you were directly accessing or overriding internal methods in versions prior to v3, your code will break.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Always include `headers: { 'Content-Type': 'application/json' }` (or appropriate type) in the `args` object for methods sending a request body (POST, PUT, PATCH).","message":"When performing POST, PUT, or PATCH requests, the 'Content-Type' header must be explicitly set in the `args.headers` parameter. Failing to do so will result in the request body not being properly sent or parsed by the remote API.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If your API returns non-standard content types or requires specific parsing, add custom parsers to the client configuration. For example, `new Client({ mimetypes: { json: ['application/json', 'application/json;charset=utf-8'] } })` to explicitly define how content types are handled.","message":"For responses other than default JSON or XML, or if a different encoding is used, you may need to configure custom response parsers or specify `mimetypes` in the `Client` options. Otherwise, the `data` callback parameter might contain unparsed raw data or incorrect types.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure the second argument to `client.post`, `client.put`, or `client.patch` is always an `args` object, even if empty, before the callback function.","cause":"Attempting to call `post`, `put`, or `patch` methods without providing the necessary `args` object.","error":"TypeError: client.post is not a function"},{"fix":"Verify the `Content-Type` header sent by the API. If it's a custom type or not correctly handled, configure custom response parsers using `client.registerParser()` or `new Client({ mimetypes: { /* ... */ } })`.","cause":"The default response parsers (JSON, XML) do not match the `Content-Type` header of the API response, or the response contains malformed data.","error":"Error: Response data is not parsed correctly. Expected JSON but received raw string/buffer."},{"fix":"For ESM projects, use dynamic `import('node-rest-client')` and then access the `Client` class, e.g., `const { Client } = await import('node-rest-client');`. Alternatively, ensure your file is treated as CommonJS (e.g., `.cjs` extension or no `\"type\": \"module\"` in `package.json`).","cause":"Attempting to use `require('node-rest-client')` in a Node.js environment configured for ECMAScript Modules (ESM) (e.g., `\"type\": \"module\"` in `package.json`).","error":"ReferenceError: require is not defined in ES module scope"}],"ecosystem":"npm"}