Skip to content

Commit cbc6c44

Browse files
committed
doc: add comments and release-notes for JSON-RPC 2.0
1 parent e7ee80d commit cbc6c44

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

doc/JSON-RPC-interface.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ major version via the `-deprecatedrpc=` command line option. The release notes
7474
of a new major release come with detailed instructions on what RPC features
7575
were deprecated and how to re-enable them temporarily.
7676

77+
## JSON-RPC 1.1 vs 2.0
78+
79+
The server recognizes [JSON-RPC v2.0](https://www.jsonrpc.org/specification) requests
80+
and responds accordingly. A 2.0 request is identified by the presence of
81+
`"jsonrpc": "2.0"` in the request body. If that key + value is not present in a request,
82+
the legacy JSON-RPC v1.1 protocol is followed instead, which was the only available
83+
protocol in previous releases.
84+
85+
|| 1.1 | 2.0 |
86+
|-|-|-|
87+
| Request marker | `"version": "1.1"` (or none) | `"jsonrpc": "2.0"` |
88+
| Response marker | (none) | `"jsonrpc": "2.0"` |
89+
| `"error"` and `"result"` fields in response | both present | only one is present |
90+
| HTTP codes in response | `200` unless there is any kind of RPC error (invalid parameters, method not found, etc) | Always `200` unless there is an actual HTTP server error (request parsing error, endpoint not found, etc) |
91+
| Notifications: requests that get no reply | (not supported) | Supported for requests that exclude the "id" field |
92+
7793
## Security
7894

7995
The RPC interface allows other programs to control Bitcoin Core,

doc/release-notes-27101.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
JSON-RPC
2+
--------
3+
4+
The JSON-RPC server now recognizes JSON-RPC 2.0 requests and responds with
5+
strict adherence to the specification (https://www.jsonrpc.org/specification):
6+
7+
- Returning HTTP "204 No Content" responses to JSON-RPC 2.0 notifications instead of full responses.
8+
- Returning HTTP "200 OK" responses in all other cases, rather than 404 responses for unknown methods, 500 responses for invalid parameters, etc.
9+
- Returning either "result" fields or "error" fields in JSON-RPC responses, rather than returning both fields with one field set to null.

src/rpc/request.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
*
2727
* 1.0 spec: http://json-rpc.org/wiki/specification
2828
* 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
29+
*
30+
* If the server receives a request with the JSON-RPC 2.0 marker `{"jsonrpc": "2.0"}`
31+
* then Bitcoin will respond with a strictly specified response.
32+
* It will only return an HTTP error code if an actual HTTP error is encountered
33+
* such as the endpoint is not found (404) or the request is not formatted correctly (500).
34+
* Otherwise the HTTP code is always OK (200) and RPC errors will be included in the
35+
* response body.
36+
*
37+
* 2.0 spec: https://www.jsonrpc.org/specification
38+
*
39+
* Also see http://www.simple-is-better.org/rpc/#differences-between-1-0-and-2-0
2940
*/
3041

3142
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)

src/rpc/util.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ std::string HelpExampleCliNamed(const std::string& methodname, const RPCArgList&
161161

162162
std::string HelpExampleRpc(const std::string& methodname, const std::string& args)
163163
{
164-
return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
164+
return "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", "
165165
"\"method\": \"" + methodname + "\", \"params\": [" + args + "]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
166166
}
167167

@@ -172,7 +172,7 @@ std::string HelpExampleRpcNamed(const std::string& methodname, const RPCArgList&
172172
params.pushKV(param.first, param.second);
173173
}
174174

175-
return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
175+
return "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", "
176176
"\"method\": \"" + methodname + "\", \"params\": " + params.write() + "}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
177177
}
178178

src/test/rpc_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ BOOST_AUTO_TEST_CASE(help_example)
551551
// test different argument types
552552
const RPCArgList& args = {{"foo", "bar"}, {"b", true}, {"n", 1}};
553553
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", args), "> bitcoin-cli -named test foo=bar b=true n=1\n");
554-
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", args), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"foo\":\"bar\",\"b\":true,\"n\":1}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
554+
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", args), "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"foo\":\"bar\",\"b\":true,\"n\":1}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
555555

556556
// test shell escape
557557
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"foo", "b'ar"}}), "> bitcoin-cli -named test foo='b'''ar'\n");
@@ -564,15 +564,15 @@ BOOST_AUTO_TEST_CASE(help_example)
564564
obj_value.pushKV("b", false);
565565
obj_value.pushKV("n", 1);
566566
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"name", obj_value}}), "> bitcoin-cli -named test name='{\"foo\":\"bar\",\"b\":false,\"n\":1}'\n");
567-
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", obj_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":{\"foo\":\"bar\",\"b\":false,\"n\":1}}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
567+
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", obj_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":{\"foo\":\"bar\",\"b\":false,\"n\":1}}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
568568

569569
// test array params
570570
UniValue arr_value(UniValue::VARR);
571571
arr_value.push_back("bar");
572572
arr_value.push_back(false);
573573
arr_value.push_back(1);
574574
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"name", arr_value}}), "> bitcoin-cli -named test name='[\"bar\",false,1]'\n");
575-
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", arr_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":[\"bar\",false,1]}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
575+
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", arr_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"2.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":[\"bar\",false,1]}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
576576

577577
// test types don't matter for shell
578578
BOOST_CHECK_EQUAL(HelpExampleCliNamed("foo", {{"arg", true}}), HelpExampleCliNamed("foo", {{"arg", "true"}}));

0 commit comments

Comments
 (0)