1

Is there a way to make bitcoin-cli output JSON-serialized errors? For example if I do

bitcoin-cli getblock foo

the output is

error code: -5
error message:
Block not found

I want the output to be something more like:

{
  "error": {
    "code": -5,
    "message": "Block not found"
  }
}

Behind the scenes, that's what the RPC response body looks like, and I want to tell bitcoin-cli, "Don't pretty-print the error if there is one. Just give me the JSON response."

  • To add some context, I want to use bitcoin-cli not curl or another http library because bitcoin-cli knows how to find and read bitcoin.conf and infer from that the RPC username, password, and port. These days with "cookie-based" authentication and INI file "sections" in bitcoin.conf, the logic to automagically infer the RPC username, password, and port is actually quite complex. The logic also differs somewhat between Bitcoin Core and other server implementations. – chrisarnesen Dec 7 '18 at 8:47
1

bitcoin-cli does not currently support dumping the raw JSON-RPC response. The relevant section of the Bitcoin Core code is here https://github.com/bitcoin/bitcoin/blob/2753285be72dd051cf2987ca5c80d0b149914020/src/bitcoin-cli.cpp#L455 .

  • Thanks to Bitcoin Core developer Pieter Wuille for answering in a comment. – chrisarnesen Dec 8 '18 at 2:01
0

try sending RPC request as a JSON object. The response will also be a JSON object.

example:

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

  • 1
    Thank you for the answer. That won't work for me though because I specifically want to use the bitcoin-cli client, not curl or another http library. In addition to knowing how to make http calls, more importantly bitcoin-cli also knows how to find and read bitcoin.conf and infer from that the RPC username, password, and port. These days with "cookie-based" authentication and INI file "sections" in bitcoin.conf, the logic to infer the username, password, and port automagically is actually quite complex. The logic is also slightly different between core and abc (regtest port for example). – chrisarnesen Dec 7 '18 at 7:48
  • I'm sorry, but bitcoin-cli does not support dumping the raw JSON-RPC response. I would really suggest trying to use an RPC library instead of bitcoin-cli, together with an rpcauth entry in the config (so you don't need to use the cookie auth). – Pieter Wuille Dec 7 '18 at 8:53
  • @PieterWuille Thank you very much for the comment, which definitely answers my question. It's funny because you actually started me down this path when you explained on GitHub that by design rpcauth doesn't disable "cookie-based" auth in order for bitcoin-cli to always "just work". I also want my library to have a "just works" mode, though I'll offer a traditional non-bitcoin-cli-based mode too. – chrisarnesen Dec 8 '18 at 1:56
  • 1
    @chrisarnesen The theory is that if you want to give access to another application, it's probably better that you need to explicitly configure it using an rpcauth entry rather than using cookie auth. – Pieter Wuille Dec 8 '18 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.