简体   繁体   中英

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.

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

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
  };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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