<p><code>bitcoin-cli</code> does not currently support dumping the raw JSON-RPC response. The relevant section of the Bitcoin Core code is here <a href="https://github.com/bitcoin/bitcoin/blob/2753285be72dd051cf2987ca5c80d0b149914020/src/bitcoin-cli.cpp#L455" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/2753285be72dd051cf2987ca5c80d0b149914020/src/bitcoin-cli.cpp#L455</a> .</p>

CC BY-SA 4.0
2018-12-08T02:01:58.300Z
Thanks to Bitcoin Core developer Pieter Wuille for answering in a comment.
Chris Arnesen
CC BY-SA 4.0
2018-12-08T02:00:52.337Z
2018-12-08T02:00:52.337Z
Chris Arnesen
<p>try sending RPC request as a JSON object. The response will also be a JSON object.</p>

<p>example: </p>

<blockquote>
  <p>curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblock", "params": ["00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"] }' -H 'content-type: text/plain;' <a href="http://127.0.0.1:8332/" rel="nofollow noreferrer">http://127.0.0.1:8332/</a></p>
</blockquote>

CC BY-SA 4.0
2018-12-07T07:48:20.543Z
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).
Chris Arnesen
CC BY-SA 4.0
2018-12-07T08:53:46.187Z
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
CC BY-SA 4.0
2018-12-08T01:56:41.323Z
@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.
Chris Arnesen
CC BY-SA 4.0
2018-12-08T02:57:45.327Z
@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
CC BY-SA 4.0
2018-12-07T06:23:04.447Z
2018-12-07T06:23:04.447Z
Akshay Dev
<p>Is there a way to make <code>bitcoin-cli</code> output JSON-serialized errors? For example if I do</p>

<pre><code>bitcoin-cli getblock foo
</code></pre>

<p>the output is</p>

<pre><code>error code: -5
error message:
Block not found
</code></pre>

<p>I want the output to be something more like:</p>

<pre><code>{
  "error": {
    "code": -5,
    "message": "Block not found"
  }
}
</code></pre>

<p>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." </p>

CC BY-SA 4.0
2018-12-07T08:47:38.337Z
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.
Chris Arnesen
<bitcoin-cli>
bitcoin-cli JSON-serialized error output
Chris Arnesen
# bitcoin-cli JSON-serialized error output

(Asked by: username_0 on 2018-12-07)

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."

**Tags:** bitcoin-cli

> **Comments:**
> **username_0**: 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.  

---

## 2 answers

### Answer by username_0 on 2018-12-08. Score: 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> .

> **Comments:**
> **username_0**: Thanks to Bitcoin Core developer username_2 username_2 for answering in a comment.  

---

### Answer by username_1 on 2018-12-07. Score: 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/>

> **Comments:**
> **username_0**: 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).  
> **username_2**: 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).  
> **username_0**: @username_2 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.  
> **username_2**: @username_0 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.  

---

