大猩猩 jsonrpc 得到空响应。

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

Gorilla jsonrpc getting empty response

问题

为什么我的jsonrpc方法返回一个空响应?

  1. type Args struct {
  2. A, B int
  3. }
  4. type Response struct {
  5. sum int
  6. message string
  7. }
  8. type Arith int
  9. func (t *Arith) Add(r *http.Request, args *Args, reply *Response) error {
  10. reply.sum = args.A + args.B
  11. reply.message = "Do math"
  12. // 这种方式也不起作用
  13. //*reply = Response{
  14. // sum : 12,
  15. // message : "Do math",
  16. //}
  17. return nil
  18. }

请求:

  1. {"method":"Arith.Add","params":[{"A": 10, "B":2}], "id": 1}

响应:

  1. {
  2. "result": {},
  3. "error": null,
  4. "id": 1
  5. }

然而,如果我将reply的类型设置为*string,那么它将正常工作:

  1. *reply = "Responding with strings works"

响应:

  1. {
  2. "result": "Responding with strings works",
  3. "error": null,
  4. "id": 1
  5. }

我正在使用http://www.gorillatoolkit.org/pkg/rpc。

英文:

Why does my jsonrpc method return an empty response?

  1. type Args struct {
  2. A, B int
  3. }
  4. type Response struct {
  5. sum int
  6. message string
  7. }
  8. type Arith int
  9. func (t *Arith) Add(r *http.Request, args *Args, reply *Response) error {
  10. reply.sum = args.A + args.B
  11. reply.message = "Do math"
  12. // this does not work either
  13. //*reply = Response{
  14. // sum : 12,
  15. // message : "Do math",
  16. //}
  17. return nil
  18. }

Request:

  1. {"method":"Arith.Add","params":[{"A": 10, "B":2}], "id": 1}

Response:

  1. {
  2. "result": {},
  3. "error": null,
  4. "id": 1
  5. }

However, if I set the type of reply to *string, then this will work fine:

  1. *reply = "Responding with strings works"

Response:

  1. {
  2. "result": "Responding with strings works",
  3. "error": null,
  4. "id": 1
  5. }

I'm using http://www.gorillatoolkit.org/pkg/rpc.

答案1

得分: 3

你的Response字段是不可导出的。名称应该是大写的:

  1. type Response struct {
  2. Sum int
  3. Message string
  4. }
英文:

Your Response fields are unexported. The names should be uppercase:

  1. type Response struct {
  2. Sum int
  3. Message string
  4. }

huangapple
  • 本文由 发表于 2015年8月26日 18:50:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/32224644.html
匿名

发表评论

匿名网友

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

确定