JSON RPC - What is the “id” for?

2020-05-31 09:09发布

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}

标签: json json-rpc
5条回答
兄弟一词,经得起流年.
2楼-- · 2020-05-31 09:37

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

查看更多
倾城 Initia
3楼-- · 2020-05-31 09:42
  1. To let the server know you're expecting a response.
  2. To match responses to requests when using asynchronous or batch calls.
查看更多
我想做一个坏孩纸
4楼-- · 2020-05-31 09:51

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.

查看更多
The star\"
5楼-- · 2020-05-31 09:51

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.

查看更多
再贱就再见
6楼-- · 2020-05-31 09:55

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.

查看更多
登录 后发表回答