【发布时间】:2013-07-01 09:23:43
【问题描述】:
我正在研究使用 gorilla web toolkit 创建一个简单的 RPC API。我正在使用他们文档中的示例,我正在 Chrome 中使用 Advanced Rest Client 进行测试并使用
http://localhost:1111/api/
并发布以下 RAW 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)
}
【问题讨论】:
-
1.缩进你的代码 2. 检查返回的错误 3. 再试一次
-
你有没有让这个工作?