request - How to properly call JSON-RPC in Go? -
request - How to properly call JSON-RPC in Go? -
i've been trying various configurations in order phone call a simple json-rpc server bitcoin in go, didn't manage anywhere.
in python, entire code looks like:
from jsonrpc import serviceproxy access = serviceproxy("http://user:pass@127.0.0.1:8332") print access.getinfo()
but in go, seem bumping erros "too many colons in address", or "no such host". i've tried using both of packages rpc , rpc/jsonrpc, using methods dial , dialhttp, using various network parameters , still can't anywhere.
so, how phone call json-rpc server in go?
the jsonrpc bundle doesn't back upwards json-rpc on http @ moment. so, can't utilize that, sorry.
but jsonrpc specification quite simple , it's quite easy write own jsonrpchttp
(oh, hope know improve name) package.
i able phone call "getinfo" succesfully using next (horrible) code:
package main import ( "encoding/json" "io/ioutil" "log" "net/http" "strings" ) func main() { data, err := json.marshal(map[string]interface{}{ "method": "getinfo", "id": 1, "params": []interface{}{}, }) if err != nil { log.fatalf("marshal: %v", err) } resp, err := http.post("http://bob:secret@127.0.0.1:8332", "application/json", strings.newreader(string(data))) if err != nil { log.fatalf("post: %v", err) } defer resp.body.close() body, err := ioutil.readall(resp.body) if err != nil { log.fatalf("readall: %v", err) } result := make(map[string]interface{}) err = json.unmarshal(body, &result) if err != nil { log.fatalf("unmarshal: %v", err) } log.println(result) }
maybe can clean bit implementing rpc.clientcodec interface (see jsonrpc/client.go example). can take advantage of go's rpc package.
json request go rpc json-rpc
Comments
Post a Comment