没有来自gorilla/rpc JSON RPC服务的响应

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

No response from gorilla/rpc JSON RPC Service

问题

我正在调查使用golang web工具包来创建一个简单的RPC API。我正在使用他们文档中的示例,并且在Chrome中使用Advanced Rest Client进行测试,使用以下URL进行POST请求:

http://localhost:1111/api/

并且发送以下原始JSON数据:

{"method":"HelloService.Say","params":[{"Who":"Test"}]}

这个请求到达了服务器,我知道这一点是因为我在记录日志(见下面的代码),并且我收到了一个200 OK的响应。然而,我得到了“响应不包含任何数据”的错误。

我期望得到在下面的Say方法中定义的JSON回复消息。有人对问题有什么建议吗?

package main

import (
    "gorilla/mux"
    "gorilla/rpc"
    "gorilla/rpc/json"
    "log"
    "net/http"
)  

type HelloArgs struct {
    Who string
}

type HelloReply struct {
    Message string
}

type HelloService struct{}

func (h *HelloService) Say(r *http.Request, args *HelloArgs, reply *HelloReply) error {
    log.Printf(args.Who)
    reply.Message = "Hello, " + args.Who + "!"
    log.Printf(reply.Message)
    return nil
}

func main() {
    r := mux.NewRouter()    
    jsonRPC := rpc.NewServer()
    jsonCodec := json.NewCodec()
    jsonRPC.RegisterCodec(jsonCodec, "application/json")
    jsonRPC.RegisterCodec(jsonCodec, "application/json; charset=UTF-8") // For firefox 11 and other browsers which append the charset=UTF-8
    jsonRPC.RegisterService(new(HelloService), "")
    r.Handle("/api/", jsonRPC)    
    http.ListenAndServe(":1111", r)
}
英文:

I'm investigating using the gorilla web toolkit to create a simple RPC API. I'm using the example from their documentation and I'm testing using Advanced Rest Client in Chrome and use

http://localhost:1111/api/ 

and POST the following RAW JSON payload:

{"method":"HelloService.Say","params":[{"Who":"Test"}]}

This reaches the server, I know this as I'm logging it (see code below) and I get a 200 OK response. However I'm getting "Response does not contain any data"

I'm expecting the JSON reply message that is defined in the Say method below. Does anyone have any suggestions as to what the problem is?

package main

import (
    "gorilla/mux"
    "gorilla/rpc"
    "gorilla/rpc/json"
    "log"
    "net/http"
)  

type HelloArgs struct {
    Who string
}

type HelloReply struct {
    Message string
}

type HelloService struct{}

func (h *HelloService) Say(r *http.Request, args *HelloArgs, reply *HelloReply) error {
    log.Printf(args.Who)
    reply.Message = "Hello, " + args.Who + "!"
    log.Printf(reply.Message)
    return nil
}

func main() {
    r := mux.NewRouter()	
    jsonRPC := rpc.NewServer()
    jsonCodec := json.NewCodec()
    jsonRPC.RegisterCodec(jsonCodec, "application/json")
    jsonRPC.RegisterCodec(jsonCodec, "application/json; charset=UTF-8") // For firefox 11 and other browsers which append the charset=UTF-8
    jsonRPC.RegisterService(new(HelloService), "")
    r.Handle("/api/", jsonRPC)	
    http.ListenAndServe(":1111", r)
}

答案1

得分: 6

这是因为gorilla/rpc/json实现了JSON-RPC,它在请求中需要三个参数:methodparamsid

在JSON-RPC中,没有id的请求被称为notifications,并且没有响应。

请查看规范以获取更多详细信息。

所以,在你的情况下,你需要使用以下JSON:

{"method":"HelloService.Say","params":[{"Who":"Test"}], "id":"1"}
英文:

It's because gorilla/rpc/json implements JSON-RPC, which requires three parameters in the request: method, params and id.

Requests without id in JSON-RPC are called notifications and do not have responses.

Check specification for more details.

So, in your case, you need to use following JSON:

{"method":"HelloService.Say","params":[{"Who":"Test"}], "id":"1"}

huangapple
  • 本文由 发表于 2013年7月3日 23:34:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/17452199.html
匿名

发表评论

匿名网友

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

确定