Вызовите JSON-RPC, используя nodejs - PullRequest
0 голосов
/ 04 мая 2018

Я пытаюсь вызвать вызов json-rpc через модуль запроса nodejs.

вызов json-rpc выполняется в следующем формате

curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getreceivedbyaddress", "params": ["1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ", 6] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/

Как вызвать такой вызов json-rpc, используя запрос npm-пакет nodejs ??

1 Ответ

0 голосов
/ 04 мая 2018

Вот скрипт, который сделает вызов с помощью модуля запроса:

index.js

const request = require('request');

// User and password specified like so: node index.js username password.
let username = process.argv.length < 2 ? "default-username" : process.argv[2];
let password = process.argv.length < 3 ? "default-password" : process.argv[3];

let options = {
    url: "http://localhost:8332",
    method: "post",
    headers:
    { 
     "content-type": "text/plain"
    },
    auth: {
        user: username,
        pass: password
    },
    body: JSON.stringify( {"jsonrpc": "1.0", "id": "curltest", "method": "getreceivedbyaddress", "params": ["1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ", 6] })
};

request(options, (error, response, body) => {
    if (error) {
        console.error('An error has occurred: ', error);
    } else {
        console.log('Post successful: response: ', body);
    }
});

А потом назови так:

node index.js username password

Вы также можете использовать переменные окружения для передачи имени пользователя / пароля.

Аргумент --auth, передаваемый Curl, определяет обычную аутентификацию (как реализовано в сценарии)

...