响应我使用gorilla web toolkit创建一个简单的RPC API调查。我使用的是从他们的文档的例子,我测试使用Advanced Rest Client在Chrome和使用没有从大猩猩/ RPC JSON RPC服务
http://localhost:1111/api/
和POST以下RAW JSON有效载荷:
{"method":"HelloService.Say","params":[{"Who":"Test"}]}
这到达服务器,我知道这当我记录它时(见下面的代码),我得到了200 OK响应。但是我得到“响应不包含任何数据”
我期待的是在下面的说法定义的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.尝试再次 – thwd
你有没有得到这个工作? – rem7