2.0.0 • Published 2 years ago

node-jsonrpc-client v2.0.0

Weekly downloads
467
License
ISC
Repository
github
Last release
2 years ago

node-jsonrpc-client npm version

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");

// Our API server is at http://example.org/api
const client = new JsonRpc("http://example.org/api");
// Let's call the 'saySomething' method that takes two parameters, 'to' and 'message'
client.call("saySomething", { to: "Alice", message: "Hi, Bob!" })
  .then((result) => {
    // The 'saySomething' method has a field 'output'
    console.log("output", result.output);
  })
  .catch((err) => {
    // oops, something went wrong!
    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()

// Our API server is at http://example.org/api
const client = new JsonRpc("http://example.org/api");
// Let's call the 'login' method that takes two parameters, 'username' and 'password'
client.call("login", { username: "alice", password: "monkey" }, cookieJar)
  .then((loginResult) => {
    // The 'getMessages' method has a field 'messages' and requires the cookie from login
    client.call("getMessages", {}, cookieJar).then((msgResult) => {
      console.log("Messages: ", result.messages);
    });
  })
  .catch((err) => {
    // oops, something went wrong!
    console.error("Oops! Error code " + err.code + ": " + err.message);
  });
2.0.0

2 years ago

1.0.4

3 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago