2013-07-03 108 views
2

响应我使用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

1.缩进你的代码 2.检查返回的错误 3.尝试再次 – thwd

+0

你有没有得到这个工作? – rem7

回答

5

这是因为大猩猩/ RPC/JSON实现JSON-RPC,这需要在该请求三个参数:方法PARAMSID

在JSON-RPC中没有ID的请求被称为通知并且没有响应。

检查​​了解更多详情。

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

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

即使我们错过的ID,我们将得到200 OK,但在体内无JSON。感谢您的回答。 :) –