What is the correct name for JSON-RPC in Go?

I tried various configurations to call a simple JSON-RPC server for Bitcoin in Go, but failed anywhere.

In Python, all the code looks like this:

from jsonrpc import ServiceProxy access = ServiceProxy("http://user: pass@127.0.0.1 :8332") print access.getinfo() 

But in Go, I seem to stumble upon erros, for example "too many colons in an address" or "there is no such host." I tried to use both rpc and rpc / jsonrpc packages using the Dial and DialHTTP methods using various network parameters and still can not get anywhere.

So, how to properly call the JSON-RPC server in Go?

+6
source share
1 answer

The jsonrpc package does not support json-rpc over HTTP at the moment. So you cannot use this, sorry.

But the jsonrpc specification is pretty simple, and it's probably pretty easy to write your own jsonrpchttp package (oh, I hope you know a better name).

I was able to successfully call "getinfo" using the following (terrible) 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) } 

Perhaps you can clean it up a bit by implementing the rpc.ClientCodec interface (see jsonrpc / client.go for an example). Then you can use the Go rpc package.

+9
source

Source: https://habr.com/ru/post/906349/


All Articles