douyu1656
douyu1656
2016-04-13 21:45
浏览 170
已采纳

使用curl调用golang调用jsonrpc

I've "hello world" rpc service written on golang. It works fine and go jsonrpc client is working. But I need to send request with curl and this example doesn't work:

curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"id": 1, "method": "Test.Say", "params": [{"greet": "world"}]}' \
http://localhost:1999/_goRPC_

Go accept connection but produce absolutely no result:

curl: (52) Empty reply from server 

Here my go code:

package main

import (
  "log"
  "os"
  "time"
  "net"
  "net/rpc"
  "net/rpc/jsonrpc"
)

// RPC Api structure
type Test struct {}

// Greet method arguments
type GreetArgs struct {
  Name string
}

// Grret message accept object with single param Name
func (test *Test) Greet(args *GreetArgs, result *string) (error) {
  *result = "Hello " + args.Name
  return nil
}

// Start server with Test instance as a service
func startServer(ch chan<- bool, port string) {
  test := new(Test)

  server := rpc.NewServer()
  server.Register(test)

  listener, err := net.Listen("tcp", ":" + port)

  if err != nil {
      log.Fatal("listen error:", err)
  }

  defer listener.Close()

  for {
      conn, err := listener.Accept()

      if err != nil {
          log.Fatal(err)
      }

      go server.ServeCodec(jsonrpc.NewServerCodec(conn))
      ch <- true
  }
}

// Start client and call Test.Greet method
func startClient(port string) {
  conn, err := net.Dial("tcp", ":" + port)

  if err != nil {
      panic(err)
  }
  defer conn.Close()

  c := jsonrpc.NewClient(conn)

  var reply string
  var args = GreetArgs{"world"}
  err = c.Call("Test.Greet", args, &reply)
  if err != nil {
      log.Fatal("arith error:", err)
  }
  log.Println("Result: ", reply)
}

func main() {
  if len(os.Args) < 2 {
    log.Fatal("port not specified")
  }

  port := os.Args[1]
  ch := make(chan bool)

  go startServer(ch, port)
  time.Sleep(500 * time.Millisecond)
  go startClient(port)

  // Produce log message each time connection closes
  for {
    <-ch
    log.Println("Closed")
  }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongwei3866
    dongwei3866 2016-04-14 02:07
    已采纳

    The jsonrpc package doesn't support json-rpc over HTTP currently. So, you can't call jsonrpc with curl. If you really want to do that, you can make a HTTP handler that adapts the HTTP request/response to a ServerCodec. For example:

    package main
    
    import (
        "io"
        "log"
        "net"
        "net/http"
        "net/rpc"
        "net/rpc/jsonrpc"
        "os"
    )
    
    type HttpConn struct {
        in  io.Reader
        out io.Writer
    }
    
    func (c *HttpConn) Read(p []byte) (n int, err error)  { return c.in.Read(p) }
    func (c *HttpConn) Write(d []byte) (n int, err error) { return c.out.Write(d) }
    func (c *HttpConn) Close() error                      { return nil }
    
    // RPC Api structure
    type Test struct{}
    
    // Greet method arguments
    type GreetArgs struct {
        Name string
    }
    
    // Grret message accept object with single param Name
    func (test *Test) Greet(args *GreetArgs, result *string) error {
        *result = "Hello " + args.Name
        return nil
    }
    
    // Start server with Test instance as a service
    func startServer(port string) {
        test := new(Test)
    
        server := rpc.NewServer()
        server.Register(test)
    
        listener, err := net.Listen("tcp", ":"+port)
    
        if err != nil {
            log.Fatal("listen error:", err)
        }
    
        defer listener.Close()
        http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    
            if r.URL.Path == "/test" {
                serverCodec := jsonrpc.NewServerCodec(&HttpConn{in: r.Body, out: w})
                w.Header().Set("Content-type", "application/json")
                w.WriteHeader(200)
                err := server.ServeRequest(serverCodec)
                if err != nil {
                    log.Printf("Error while serving JSON request: %v", err)
                    http.Error(w, "Error while serving JSON request, details have been logged.", 500)
                    return
                }
            }
    
        }))
    }
    
    func main() {
        if len(os.Args) < 2 {
            log.Fatal("port not specified")
        }
    
        port := os.Args[1]
    
        startServer(port)
    }
    

    Now you can call it with curl -X POST -H "Content-Type: application/json" -d '{"id": 1, "method": "Test.Greet", "params": [{"name":"world"}]}' http://localhost:port/test

    Part of the code is from this post

    点赞 评论
  • duanlipeng4136
    duanlipeng4136 2016-04-14 02:43

    @jfly has a nifty solution.

    Another option, if you still wanted to test with something besides the go jsonrpc cient (probably the easiest option), or use @jfly's answer, is you can use telnet to send raw data:

    computer:~ User$ telnet 127.0.0.1 8888
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    {"method":"Test.Greet","params":[{"Name":"world"}],"id":0}
    {"id":0,"result":"Hello world","error":null}
    {"method":"Test.Greet","params":[{"Name":"world"}],"id":0}
    {"id":0,"result":"Hello world","error":null}
    {"method":"Test.Greet","params":[{"Name":"world"}],"id":0}
    {"id":0,"result":"Hello world","error":null}
    

    The above is the output including payload I typed in and your server's responses.

    tcpdump was my friend when I was figuring out the right payload to send.

    点赞 评论

相关推荐