I am writing a JSON RPC conform js library. Are clients allowed by the specification to add custom fields (e.g. headers
) along the params
field to Request
objects?
E.g.
{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "headers": { "original-client": 12 }, "id": 1}
The spec (https://www.jsonrpc.org/specification#request_object) does not mention anything about adding additional fields.
Does my library violate the spec if it does add an header
field?
Same question here. (I want to add user auth info / message signature...)
You are right in that the Specification does not mention anything about additional fields. So legally-speaking, if it is not forbidden, it is allowed.
On the other hand, I found at least one reference implementation here: www.simple-is-better.org/rpc/jsonrpc.py, which will hit you with an exception if a decoded message contains extra fields. This code is from one of the authors of the JSON-RPC 2.0 specs, Roland Köbler.
So, YMMV.
Edit: I solved this using the following protocol:
"rpc.secinfo"
. (The rpc.
prefix is per-spec reserved for extensions)"params"
field contains whatever metadata applies (e.g. username, signature){"jsonrpc": "2.0", "method": "rpc.secinfo", "params": {"user":"root", "signature":"0xDEADBEEF"}}<DELIM><payload><DELIM>
The receiver can decode the header message just like any other call, and just needs to apply (check sig, whatever) the header onto the following message.
Discussion:
Feel free to reuse this protocol, but please credit this SO answer.