https://github.com/adamslevy/jsonrpc2
Golang package for implementing a JSON RPC 2.0 server or client.
https://github.com/adamslevy/jsonrpc2
Last synced: about 1 month ago
JSON representation
Golang package for implementing a JSON RPC 2.0 server or client.
- Host: GitHub
- URL: https://github.com/adamslevy/jsonrpc2
- Owner: AdamSLevy
- License: mit
- Created: 2018-06-27T09:52:00.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-06T08:16:47.000Z (almost 2 years ago)
- Last Synced: 2025-04-14T11:07:23.652Z (about 1 month ago)
- Language: Go
- Homepage:
- Size: 124 KB
- Stars: 11
- Watchers: 1
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# github.com/AdamSLevy/jsonrpc2/v14
[](https://godoc.org/github.com/AdamSLevy/jsonrpc2)
[](https://goreportcard.com/report/github.com/AdamSLevy/jsonrpc2)
[](https://coveralls.io/github/AdamSLevy/jsonrpc2?branch=master)
[](https://travis-ci.org/AdamSLevy/jsonrpc2)Package jsonrpc2 is a complete and strictly conforming implementation of the
JSON-RPC 2.0 protocol for both clients and servers.The full specification can be found at https://www.jsonrpc.org.
## Clients
The simplest way to make a JSON-RPC 2.0 request is to use the provided
Client.Request.
```golang
var c jsonrpc2.Client
params := []float64{1, 2, 3}
var result int
err := c.Request(nil, "http://localhost:8080", "sum", params, &result)
if _, ok := err.(jsonrpc2.Error); ok {
// received Error Request
}
if err != nil {
// some JSON marshaling or network error
}
fmt.Printf("The sum of %v is %v.\n", params, result)
```For clients that do not wish to use the provided Client, the Request and
Response types can be used directly.```golang
req, _ := json.Marshal(jsonrpc2.Request{Method: "subtract",
Params: []int{5, 1},
ID: 0,
})
httpRes, _ := http.Post("www.example.com", "application/json",
bytes.NewReader(req))
resBytes, _ := ioutil.ReadAll(httpRes.Body)
res := jsonrpc2.Response{Result: &MyCustomResultType{}, ID: new(int)}
json.Unmarshal(respBytes, &res)
```## Servers
Servers define their own MethodFuncs and associate them with a name in a
MethodMap that is passed to HTTPRequestHandler() to return a corresponding
http.HandlerFunc. See HTTPRequestHandler for more details.
```golang
func getUser(ctx context.Context, params json.RawMessage) interface{} {
var u User
if err := json.Unmarshal(params, &u); err != nil {
return jsonrpc2.InvalidParams(err)
}
conn, err := mydbpkg.GetDBConn()
if err != nil {
// The handler will recover, print debug info if enabled, and
// return an Internal Error to the client.
panic(err)
}
if err := u.Select(conn); err != nil {
return jsonrpc2.NewError(-30000, "user not found", u.ID)
}
return u
}func StartServer() {
methods := jsonrpc2.MethodMap{"version": versionMethod}
http.ListenAndServe(":8080", jsonrpc2.HTTPRequestHandler(methods))
}
```