pcwu's TIL Notes


[RPC] JSON-RPC 簡介

Intro

最近碰到的程式是用 JSON-RPC 透過 HTTP 傳輸的,沒使用過覺得滿特別的,於是來筆記一下。(自己孤漏寡聞沒用過就說人家特別XD)

JSON-RPC

Over HTTP

JSON-RPC 規範一開頭就提到:

It is transport agnostic in that the concepts can be used within the same process, over sockets, over http, or in many various message passing environments.

所以是可以透過單純 HTTP 的 POST 方法來發出 REQUEST

JSON-RPC 2.0 Request

Request 需要包含一下欄位:

JSON-RPC 2.0 Response

Example

--> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
<-- {"jsonrpc": "2.0", "result": 19, "id": 3}

--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
<-- {"jsonrpc": "2.0", "result": 19, "id": 4}

Conclusion

這篇文章只極簡單列了用得到的部份,其他部份請參考下面的連結文件。

Reference