Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noot/go-json-rpc
very small lib to call a json-rpc endpoint
https://github.com/noot/go-json-rpc
Last synced: about 2 months ago
JSON representation
very small lib to call a json-rpc endpoint
- Host: GitHub
- URL: https://github.com/noot/go-json-rpc
- Owner: noot
- License: lgpl-3.0
- Created: 2022-10-13T21:54:49.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2022-10-13T23:18:45.000Z (about 2 years ago)
- Last Synced: 2024-10-15T22:27:12.084Z (3 months ago)
- Language: Go
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-json-rpc
Very small lib for calling a JSON-RPC server. I kept copy pasting this code so I just put it in its own repo :)
## Usage
```go
package mainimport (
"encoding/json"
"fmt"rpc "github.com/noot/go-json-rpc"
)type MyRequestType struct {
SomeParam string
}type MyResponseType struct {
SomeReturnValue string
}func main() {
resp, err := callSomething("http://localhost:8545", "this is an example")
if err != nil {
panic(err)
}fmt.Println(resp)
}func callSomething(endpoint, someParam string) (*MyResponseType, error) {
const method = "server_someMethod"req := &MyRequestType{
SomeParam: someParam,
}params, err := json.Marshal(req)
if err != nil {
return nil, err
}resp, err := rpc.PostRPC(endpoint, method, string(params))
if err != nil {
return nil, err
}if resp.Error != nil {
return nil, resp.Error
}var res *MyResponseType
if err = json.Unmarshal(resp.Result, &res); err != nil {
return nil, err
}return res, nil
}
```