英文:
Dialing a jsonrpc connection to bitcoin
问题
package main
import "rpc/jsonrpc"
import "fmt"
func main() {
rc, e := jsonrpc.Dial("tcp", "user:pass@localhost:8332")
if e != nil {fmt.Print(e);return;}
var blocks float64
rc.Call("getblockcount", "", &blocks)
if e != nil {fmt.Print(e); return;}
fmt.Print("%f blocks", blocks)
}
英文:
package main
import "rpc/jsonrpc"
import "fmt"
func main() {
rc, e := jsonrpc.Dial("tcp", "user:pass@localhost:8332")
if e != nil {fmt.Print(e);return;}
var blocks float64
rc.Call("getblockcount", "", &blocks)
if e != nil {fmt.Print(e); return;}
fmt.Print("%f blocks", blocks)
}
Gives me the following error:
dial tcp user:pass@localhost:8332: too many colons in address user:pass@localhost:8332
How do I authenticate my rpc connection?
答案1
得分: 2
Go的rpc/jsonrpc包(以及更一般的rpc包)不支持身份验证。可以在底层net.Dial函数的第二个参数的文档中找到jsonrpc.Dial的有效字符串。
但我认为你还做了一个很大的假设,即你尝试连接的任何系统(比特币可能?)都支持Go的jsonrpc协议,除非它是用Go编写的,否则几乎肯定不支持。
英文:
The Go rpc/jsonrpc package (and more generally, the rpc package) does not support authentication. A valid string for the jsonrpc.Dial can be found in the documentation for the second argument of the underlying net.Dial function.
But I think you're also making a big assumption that whatever system you're trying to connect to (bitcoin maybe?) supports the Go jsonrpc protocol, which--unless it's written in Go--it almost assuredly does not.
答案2
得分: 2
testRequest := {"jsonrpc": "1.0", "id":"", "method": "help", "params": []}
request, e := http.NewRequest("POST", brpc.addr, strings.NewReader(testRequest))
request.SetBasicAuth(brpc.user, brpc.pass)
responce, e := brpc.c.Do(request)
// responce.Body has the result
英文:
testRequest := `{"jsonrpc": "1.0", "id":"", "method": "help", "params": []}`
request, e := http.NewRequest("POST", brpc.addr, strings.NewReader(testRequest))
request.SetBasicAuth(brpc.user, brpc.pass)
responce, e := brpc.c.Do(request)
// responce.Body has the result
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论