I want to send object of custom struct in server to server request . but dont sucssessfully send object having error on rpc hook " unexpected end of JSON input"
This is my code outside nakama server
a := UpdateSpinningWheelValues{1,"test"}
showDownDAta, err := json.Marshal(a)
response, err := http.Post("http://127.0.0.1:7350/v2/rpc/http_handler_path?http_key=defaultkey", "application/json", bytes.NewBuffer(showDownDAta))
if err != nil {
//handle error
}
here is my code in nakama init module
func HttpHandler(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
decoded := &UpdateSpinningWheelValues{}
if err := json.Unmarshal([]byte(payload), &decoded); err != nil {
logger.Printf("err is :: %v ", err)
} else {
logger.Printf("Index is :: %v ", decoded)
}
return "testing http", nil
}
Error i get “unexpected end of json input in golang”
Hello @HashtagGo,
What version of Nakama server are you using?
Currently i’m using image of nakama 2.5.1
Happened to me too, only Nakama 2.7 can receive raw json via RPC.
As a workaround for previous versions you can send the json as a string, to do that you must enclose the payload with quotes on the client side. On the server side the code should stay the same.
when i use this its work fine
showDownDAta, err := json.Marshal(“a”) // workfine here
response, err := http.Post(“http://127.0.0.1:7350/v2/rpc/http_handler_path?http_key=defaultkey”, “application/json”, bytes.NewBuffer(showDownDAta))
if err != nil {
//handle error
}
but then send a struct its get an error
a := UpdateSpinningWheelValues{1,“test”}
showDownDAta, err := json.Marshal(a) //problem in this case
response, err := http.Post(“http://127.0.0.1:7350/v2/rpc/http_handler_path?http_key=defaultkey”, “application/json”, bytes.NewBuffer(showDownDAta))
if err != nil {
//handle error
}
First one is just a string, which json converts to a quote enclosed string for the payload, the second one is a struct which converts to a json object. Just marshal the struct on the client side, then you must add quotes at the beggining and end for the Nakama gateway to interpret it as a string and allow it.
@Superxwolf You’re right with older versions of Nakama (before 2.7.0) but in this release and all newer releases you can add unwrap
as a query param to your server to server HTTP request so that it doesn’t need to be an escaped JSON string as input.
i.e.
"http://127.0.0.1:7350/v2/rpc/http_handler_path?http_key=defaultkey&unwrap=true"