gpt4 book ai didi

JSON-RPC, "cannot unmarshal object"

转载 作者:数据小太阳 更新时间:2023-10-29 03:13:21 28 4
gpt4 key购买 nike

我正在尝试了解 JSON-RPC 的工作原理,并正在使用 Go 语言 (golang) 对其进行测试。 Go 程序运行良好。它做了它应该做的。但是当我尝试通过 telnet 发出原始请求时,它给出了一个错误。

这里描述了工作和 super 简单的 JSON-RPC 服务器:

    // rpc_json_server.go

package main

import (
"log"
"net"
"net/http"
"net/rpc"
"net/rpc/jsonrpc"
)

//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------

type Arithmetic int // Used as RPC Service called 'Arithmetic'

type Arguments struct {
A int
B int
}

type Result int

//------------------------------------------------------------------------------
// Methods
//------------------------------------------------------------------------------

func (t *Arithmetic) Multiply(args *Arguments, res *Result) error {

*res = Result(args.A * args.B)

return nil
}

//------------------------------------------------------------------------------

func main() {

var srv *rpc.Server
var err error
var arith *Arithmetic
var listener net.Listener
var codec rpc.ServerCodec
var srv_conntype, srv_host, srv_port, srv_addr, srv_path string
var srv_debugPath string
var connection net.Conn

srv_conntype = "tcp"
srv_host = "0.0.0.0"
srv_port = "3000"
srv_addr = srv_host + ":" + srv_port
srv_path = "/"
srv_debugPath = "/debug"

// Create Server, register Service
srv = rpc.NewServer()
arith = new(Arithmetic)
err = srv.Register(arith)
if err != nil {
log.Fatalf("Error. Service Format is not correct. %s\r\n", err) //dbg
}

// Handle, listen
srv.HandleHTTP(srv_path, srv_debugPath)
listener, err = net.Listen(srv_conntype, srv_addr)
if err != nil {
log.Fatalf("Error. Can not listen on %s. %s\r\n", srv_addr, err) //dbg
}
log.Printf("Started RPC Handler at %s.\r\n", srv_addr) //dbg

// Serve
for {

connection, err = listener.Accept()
if err != nil {
log.Fatal(err)
}

codec = jsonrpc.NewServerCodec(connection)

go srv.ServeCodec(codec)
}

err = http.Serve(listener, nil)
if err != nil {
log.Fatalf("Serve Error. %s\r\n", err) //dbg
}
}

//------------------------------------------------------------------------------

工作和 super 简单的 JSON-RPC 客户端代码如下:

    // rpc_json_client.go

package main

import (
"fmt"
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
)

//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------

type Arithmetic int // Used as RPC Service called 'Arithmetic'

type Arguments struct {
A int
B int
}

type Result int

//------------------------------------------------------------------------------
// Methods
//------------------------------------------------------------------------------

func main() {

var err error
var srv_conntype, srv_host, srv_port, srv_addr string
//var srv_path string
var client *rpc.Client
var args Arguments
var result Result
var serviceName, methodName, funcName string
var connection net.Conn

srv_conntype = "tcp"
srv_host = "0.0.0.0"
srv_port = "3000"
srv_addr = srv_host + ":" + srv_port
//srv_path = "/"

// Connect to RPC Server
connection, err = net.Dial(srv_conntype, srv_addr)
if err != nil {
log.Fatalf("Error. Can not connect to %s. %s\r\n", srv_addr, err) //dbg
}
defer connection.Close()

// Client
client = jsonrpc.NewClient(connection)

// Prepare Call
serviceName = "Arithmetic"
methodName = "Multiply"
funcName = serviceName + "." + methodName
args.A = 7
args.B = 8

// Call remote Procedure
err = client.Call(funcName, args, &result)
if err != nil {
log.Fatalf("Error. %s\r\n", err) //dbg
}

// Show Results
fmt.Printf("[%d; %d] -> [%d].\r\n", args.A, args.B, result) //dbg
}

再来一次。这个 golang 程序运行良好。

但接下来我无法理解。

    telnet localhost 3000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
{
"jsonrpc":"2.0",
"method":"Arithmetic.Multiply",
"params": { "A": 5, "B": 6 },
"id":1
}
{"id":1,"result":null,"error":"json: cannot unmarshal object into Go value of type [1]interface {}"}

请给我一些建议或出现此类错误的原因。原始请求有什么问题?

提前致谢!

最佳答案

您的代码看起来不错。

但是在请求中,params应该是一个包含实际参数的数组。

尝试使用以下有效负载,它应该可以工作:

{ 
"jsonrpc":"2.0",
"method":"Arithmetic.Multiply",
"params": [ { "A": 5, "B": 6 } ],
"id":1
}

(注意包含实际参数的“[”和“]”)

关于JSON-RPC, "cannot unmarshal object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44892236/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com