6

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:

from jsonrpc import ServiceProxy
access = ServiceProxy("http://user:pass@127.0.0.1:8332")
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?

Community
  • 1
  • 1
ThePiachu
  • 8,695
  • 17
  • 65
  • 94
  • wasn't this the solution to your problem? http://stackoverflow.com/questions/8854682/go-json-rpc-too-many-colons – thwd Jan 18 '12 at 23:04
  • No, that solved only a part of the problem I think, I still get error of "no such host" and other ones depending on which of many different configurations I tried. That's why I'd appreciate some working code, as checking options such as whether to put "http://" in the address, which network to use, whether to use rpc or jsonrpc, whether to call Dial or DialHTTP produces way too many options to try and tweak all of them. – ThePiachu Jan 18 '12 at 23:11

1 Answers1

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:

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 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.

tux21b
  • 90,183
  • 16
  • 117
  • 101
  • Hmm, you seem to have some old imports that changed a bit - "encoding/json" to "json" and "net/http" to "http". Asides that, thanks a lot! As for writing my own library, well, I'd like to do that, but I don't think I understand what should go where in the jsonrpc package. Probably should get some repo started though, since as I can see there are a couple people having the same problem. – ThePiachu Jan 19 '12 at 23:54
  • I am using the latest tip at the moment. Those imports will change pretty soon when Go 1 is released. Btw, I have also submitted a bug report for this on the Go issue tracker, but the priority was changed to "Later", so a separate package might be a good idea at the moment. – tux21b Jan 20 '12 at 09:04
  • Updated the code with a server implementation. Don't know much about the rpc codecs though, so based my implementation on http package a bit (registering functions to be called for specific methods and then executing them). – ThePiachu Jan 21 '12 at 02:30