简体   繁体   English

如何使用 Adonis 发出 json-rpc 请求

[英]How to make json-rpc request with Adonis

Am using Adonis to build a Bitcoin RPC system, so am making request with request.js Lib, so but the issue is with the callback when I make the request it works but I can't see send the response to the web endpoint, when I console the response from the RPC server it works fine but on postman it is blank.我正在使用 Adonis 构建比特币 RPC 系统,所以正在使用request.js Lib 发出请求,所以问题出在我发出请求时的回调它有效但我看不到将响应发送到 web 端点,当我控制台来自 RPC 服务器的响应它工作正常,但在 postman 上它是空白的。

getBlockCount({ response}){
    const dataString = `{"jsonrpc":"1.0","id":"curltext","method":"getblockcount","params":[]}`;
    const options = {
        url: `http://${USER}:${PASS}@${HOST}:${PORT}/`,
        method: "POST",
        headers: headers,
        body: dataString
    };
    const returnData;
    const callback = (error, nextRes, body) => {
      if (!error && nextRes.statusCode == 200) {
        const data = JSON.parse(body);
        console.log(data)
        returnData = data;
        response.status(200).send(returnData)
      }
      return response.send('data');
    };
    
    return request(options, callback);
    // const options = requestOption(dataString);
    // console.log(rpcRequest(options, callBack(response)));
}

I ended up using request-promise And this is what it look like我最终使用了request-promise这就是它的样子

async getBlockCount({req, response}){
    return await rp(requestOption(`{"jsonrpc":"1.0","id":"curltext","method":"getblockcount","params":[]}`))
  }

function requestOption(dataString) {
  return {
      url: `http://${USER}:${PASS}@${HOST}:${PORT}/`,
      method: "POST",
      headers: headers,
      body: dataString
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM