Search code examples
gorpc

service/method request ill-formed


I am new very to go and RPC and I can't seem to figure out what is causing my client to panic with:

2020/01/04 14:34:29 rpc: service/method request ill-formed: API:Greeting

Here's what my client looks like:

main.go

func main() {
    serverAddress := "localhost"
    serverPort := "4040"

    client, err := rpc.DialHTTP("tcp", serverAddress+":"+serverPort)
    if err != nil{
        log.Fatal(err)
    }

    fmt.Println("start")
    var results string

    a := "www.google.com"
    err = client.Call("API:Greeting", a, &results)

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

    fmt.Println(results)
    fmt.Println("end")
}

And here is what my server looks like (on a different project):

main.go

package main
import (
    "fmt"
    "log"
    "net"
    "net/http"
    "net/rpc"
)

type API int

func (api *API) Greeting(empty string, result *string) error {
    fmt.Println("called Greeting")
    *result = "Hello"
    fmt.Println("returning Greeting")
    return nil
}

func main() {
    serverPort := "8080"
    api := new(API)
    err := rpc.Register(api)
    if err != nil {
        log.Fatal("error registering API", err)
    }

    rpc.HandleHTTP()

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

    if err != nil {
        log.Fatal("Listener error", err)
    }
    log.Printf("serving rpc on port %s", serverPort)
    err = http.Serve(listener, nil)

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

It would be great if someone could point out what I am doing wrong.


Solution

  • I believe you have to call the remote procedure like this:

    client.Call("API.Greeting", a, &results)
    

    Note the Object.Method notation, instead of Object:Method.