[英]Call JSON-RPC using nodejs
I am trying to call json-rpc call through request module of nodejs. 我正在尝试通过nodejs的请求模块调用json-rpc调用。
json-rpc call is in following format 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/
How to call such json-rpc call using request npm package of nodejs ?? 如何使用nodejs的请求 npm包调用此类json-rpc调用?
Here's a script that will make the call using the request module: 这是一个将使用请求模块进行调用的脚本:
index.js 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);
}
});
And then call like so: 然后像这样调用:
node index.js username password
You could also use environment variables to pass the username/password. 您还可以使用环境变量来传递用户名/密码。
The --auth argument passed to Curl specifies Basic authentication (as implemented in the script) 传递给Curl的--auth参数指定基本身份验证(在脚本中实现)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.