diff options
author | Matthew Zipkin <pinheadmz@gmail.com> | 2023-07-07 14:41:23 -0400 |
---|---|---|
committer | Matthew Zipkin <pinheadmz@gmail.com> | 2024-05-14 10:39:43 -0400 |
commit | 466b90562f4785de74b548f7c4a256069e2aaf43 (patch) | |
tree | b911c6d48f8bf93c91b1c39f210a67bc8c2efee5 /src/rpc/request.cpp | |
parent | 2ca1460ae3a7217eaa8c5972515bf622bedadfce (diff) | |
download | bitcoin-466b90562f4785de74b548f7c4a256069e2aaf43.tar.xz |
rpc: Add "jsonrpc" field and drop null "result"/"error" fields
Only for JSON-RPC 2.0 requests.
Diffstat (limited to 'src/rpc/request.cpp')
-rw-r--r-- | src/rpc/request.cpp | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp index 08e0658561..b5bf1e5ffa 100644 --- a/src/rpc/request.cpp +++ b/src/rpc/request.cpp @@ -37,14 +37,21 @@ UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, return request; } -UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id) +UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id, JSONRPCVersion jsonrpc_version) { UniValue reply(UniValue::VOBJ); - if (!error.isNull()) - reply.pushKV("result", NullUniValue); - else + // Add JSON-RPC version number field in v2 only. + if (jsonrpc_version == JSONRPCVersion::V2) reply.pushKV("jsonrpc", "2.0"); + + // Add both result and error fields in v1, even though one will be null. + // Omit the null field in v2. + if (error.isNull()) { reply.pushKV("result", std::move(result)); - reply.pushKV("error", std::move(error)); + if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("error", NullUniValue); + } else { + if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("result", NullUniValue); + reply.pushKV("error", std::move(error)); + } reply.pushKV("id", std::move(id)); return reply; } |