提问人:Rodrigo 提问时间:10/24/2023 最后编辑:VolkerRodrigo 更新时间:10/26/2023 访问量:36
go-ethereum/rpc 上公开的方法似乎不起作用
Exposed method on go-ethereum/rpc doesn't seems to work
问:
我有以下工作代码,我可以使用 cURL 来验证 RMI 是否存在,但是当我尝试调用它时,它说它不存在。
我的最小可重现示例:
package main
import (
"log"
"net/http"
"github.com/ethereum/go-ethereum/rpc"
)
type ExampleService struct{}
func (e *ExampleService) Add(a, b int) int {
return a + b
}
func main() {
// Create a new RPC server
server := rpc.NewServer()
// Register a function to be called via JSON-RPC
server.RegisterName("example", new(ExampleService))
// Start an HTTP server to listen for requests
http.Handle("/", server)
log.Fatal(http.ListenAndServe(":8545", nil))
}
如您所见,RMI 示例存在:
$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"rpc_modules","params":[],"id":1}' http://localhost:8545
{"jsonrpc":"2.0","id":1,"result":{"example":"1.0","rpc":"1.0"}}
当我尝试调用它时,它失败了:
$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"example.Add","params":[3,4],"id":1}' http://localhost:8545
{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"the method example.Add does not exist/is not available"}}
我做错了什么?
答:
2赞
Rodrigo
10/24/2023
#1
发现了问题,我应该这样调用:
curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"calculator_add","params":[3, 2],"id":1}' http://localhost:8000
{"jsonrpc":"2.0","id":1,"result":5}
方法名称为:calculator_add。
评论
0赞
Rodrigo
10/26/2023
@tylerh你为什么删除我的问题?
0赞
TylerH
10/26/2023
答案是针对解决方案,而不是问题,正如我的编辑描述已经指出的那样。如果您有新问题,请使用本页顶部的“提问”按钮发布。
0赞
Rodrigo
10/26/2023
SO曾经是一个不错的地方......
评论