JSON RPC - What is the “id” for?

2020-05-31 09:26发布

问题:

I don't understand what the ID is for in JSON RPC. Also, how bad is it considered to not use JSON-RPC.org's standards when developing a toolkit? There seems to be some ambiguity in the JSON-RPC world.

P.S. The ID I'm referring to is the id in here:

{"params":["Hello","World"],"method":"hello_world","id":1}

回答1:

You're not guaranteed to get your answers back in the order you asked for them; the id is to help you sort that out.



回答2:

The "id" is returned in the corresponding response object, so you can map one context to the other.

If you are making synchronous single calls, it might not make sense, but in an async multi-outstanding-call enviroment it is vital.

It should not be hard coded to 1, but set to a unique value for every request object you generate from the client.



回答3:

None of the answers mentions the difference between the two existing versions of the protocol.

JSON RPC 1.0:

The request id. This can be of any type. It is used to match the response with the request that it is replying to.

JSON RPC 2.0:

An identifier established by the Client that MUST contain a String, Number, or NULL value if included. If it is not included it is assumed to be a notification. The value SHOULD normally not be Null and Numbers SHOULD NOT contain fractional parts.

Thus it is perfectly fine in JSON RPC 2.0 to set id to some fixed value. But be aware of the use of id in batch requests.



回答4:

  1. To let the server know you're expecting a response.
  2. To match responses to requests when using asynchronous or batch calls.


回答5:

You can read the JSON RPC docment https://www.jsonrpc.org/specification. In the "4 Request object" the id param is explain clearly.



标签: json json-rpc