node-jsonrpc-client 
A really, really simple JSON-RPC 2.0 client.
Installation
npm install --save node-jsonrpc-client
# Or
yarn add node-jsonrpc-client
Usage
Simple usage
const { JsonRpc } = require("node-jsonrpc-client");
const client = new JsonRpc("http://example.org/api");
client.call("saySomething", { to: "Alice", message: "Hi, Bob!" })
.then((result) => {
console.log("output", result.output);
})
.catch((err) => {
console.error("Oops! Error code " + err.code + ": " + err.message);
});
Using cookies
If the API is using a cookie to keep track of the session, you can use setUseCookies(true)
:
const { JsonRpc } = require("node-jsonrpc-client");
const CookieJar = require('tough-cookie')
const cookieJar = new CookieJar()
const client = new JsonRpc("http://example.org/api");
client.call("login", { username: "alice", password: "monkey" }, cookieJar)
.then((loginResult) => {
client.call("getMessages", {}, cookieJar).then((msgResult) => {
console.log("Messages: ", result.messages);
});
})
.catch((err) => {
console.error("Oops! Error code " + err.code + ": " + err.message);
});