如何在Go中正确调用JSON-RPC?

huangapple go评论135阅读模式
英文:

How to properly call JSON-RPC in Go?

问题

我一直在尝试不同的配置来调用Go中的一个简单的比特币JSON-RPC服务器,但是没有取得任何进展。

在Python中,整个代码看起来像这样:

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

但是在Go中,我似乎遇到了一些错误,比如“地址中有太多冒号”或“没有这样的主机”。我尝试使用rpc和rpc/jsonrpc两个包,使用Dial和DialHTTP方法,使用各种网络参数,但仍然无法取得任何进展。

那么,我该如何正确地调用Go中的JSON-RPC服务器?

英文:

I've been trying various configurations in order to call a simple JSON-RPC server for Bitcoin in Go, but didn't manage to get anywhere.

In Python, the entire code looks like:

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

But in Go, I seem to be bumping into erros like "too many colons in address", or "no such host". I've tried using both of the packages rpc and rpc/jsonrpc, using methods Dial and DialHTTP, using various network parameters and still can't get anywhere.

So, how do I properly call a JSON-RPC server in Go?

答案1

得分: 11

The jsonrpc package doesn't support json-rpc over HTTP at the moment. So, you can't use that, sorry.

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

I was able to call "getinfo" succesfully using the following (horrible) code:

  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "strings"
  8. )
  9. func main() {
  10. data, err := json.Marshal(map[string]interface{}{
  11. "method": "getinfo",
  12. "id": 1,
  13. "params": []interface{}{},
  14. })
  15. if err != nil {
  16. log.Fatalf("Marshal: %v", err)
  17. }
  18. resp, err := http.Post("http://bob:secret@127.0.0.1:8332",
  19. "application/json", strings.NewReader(string(data)))
  20. if err != nil {
  21. log.Fatalf("Post: %v", err)
  22. }
  23. defer resp.Body.Close()
  24. body, err := ioutil.ReadAll(resp.Body)
  25. if err != nil {
  26. log.Fatalf("ReadAll: %v", err)
  27. }
  28. result := make(map[string]interface{})
  29. err = json.Unmarshal(body, &result)
  30. if err != nil {
  31. log.Fatalf("Unmarshal: %v", err)
  32. }
  33. log.Println(result)
  34. }

Maybe you can clean it up a bit by implementing the rpc.ClientCodec interface (see jsonrpc/client.go for an example). Then you can take advantage of Go's rpc package.

英文:

The jsonrpc package doesn't support json-rpc over HTTP at the moment. So, you can't use that, sorry.

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

I was able to call "getinfo" succesfully using the following (horrible) code:

  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "strings"
  8. )
  9. func main() {
  10. data, err := json.Marshal(map[string]interface{}{
  11. "method": "getinfo",
  12. "id": 1,
  13. "params": []interface{}{},
  14. })
  15. if err != nil {
  16. log.Fatalf("Marshal: %v", err)
  17. }
  18. resp, err := http.Post("http://bob:secret@127.0.0.1:8332",
  19. "application/json", strings.NewReader(string(data)))
  20. if err != nil {
  21. log.Fatalf("Post: %v", err)
  22. }
  23. defer resp.Body.Close()
  24. body, err := ioutil.ReadAll(resp.Body)
  25. if err != nil {
  26. log.Fatalf("ReadAll: %v", err)
  27. }
  28. result := make(map[string]interface{})
  29. err = json.Unmarshal(body, &result)
  30. if err != nil {
  31. log.Fatalf("Unmarshal: %v", err)
  32. }
  33. log.Println(result)
  34. }

Maybe you can clean it up a bit by implementing the rpc.ClientCodec interface (see jsonrpc/client.go for an example). Then you can take advantage of Go's rpc package.

huangapple
  • 本文由 发表于 2012年1月19日 06:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/8918455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定