How to make json-rpc request with Adonis

352 views Asked by At

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

There are 1 answers

0
user3424387 On

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