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: 3 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 (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-10-13T23:18:45.000Z (almost 4 years ago)
- Last Synced: 2025-01-13T11:13:29.334Z (over 1 year 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 main
import (
"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
}
```