Example for calling custom RPC from Postman with payload

I’m Facing a little problem with with RPC call via postman - The error I’m getting -

{
    "error": "D:\\UnityProjects\\Server\\data\\modules\\match_handlers\\server_time.lua:16: bad argument #1 to json_decode (expects JSON string)\nstack traceback:\n\t[G]: in function 'json_decode'\n\tD:\\UnityProjects\\Server\\data\\modules\\match_handlers\\server_time.lua:16: in main chunk\n\t[G]: ?",
    "message": "D:\\UnityProjects\\Server\\data\\modules\\match_handlers\\server_time.lua:16: bad argument #1 to json_decode (expects JSON string)\nstack traceback:\n\t[G]: in function 'json_decode'\n\tD:\\UnityProjects\\Server\\data\\modules\\match_handlers\\server_time.lua:16: in main chunk\n\t[G]: ?",
    "code": 13
}

Here’s my example RPC -

--- RPC call to get server time
function s_t.get_server_time(context, payload)
    log.log_debug("[get_server_time] : context = " .. nk.json_encode(context))
    log.log_debug("[get_server_time] : type of payload = " .. type(payload))
    log.log_debug("[get_server_time] : payload = " .. payload)
    local json = nk.json_decode(payload)

    local result = {
        c_time = json.s_time,
        s_time = nk.time(),
        reset = json.reset
    }
    --log.log_norm("[server_time/get_server_time] : ----> Client time " .. result.c_time, " & sent server_time : " .. result.s_time)
    return nk.json_encode(result)
end

Server logs -

[get_server_time] : context = {"client_ip":"127.0.0.1","env":{},"execution_mode":"rpc","query_params":{}}
[get_server_time] : type of payload = string
[get_server_time] : payload =
{"level":"error","ts":"2019-07-24T13:17:44.620+0530","msg":"Runtime RPC function caused an error","id":"get_server_time","error":"D:\\UnityProjects\\Server\\data\\modules\\match_handlers\\server_time.lua:16: bad argument #1 to json_decode (expects JSON string)\nstack traceback:\n\t[G]: in function 'json_decode'\n\tD:\\UnityProjects\\Server\\data\\modules\\match_handlers\\server_time.lua:16: in main chunk\n\t[G]: ?","stacktrace":"github.com/heroic ...........................................

I’m passing this data as raw "{\"s_time\": 123456}" with content type application/json .
I’ve tried several variations for sending raw data too like this one - {"s_time": 123456} but same error.
Want to have an example on how to properly send payload to custom RPC via Postman.

Hey @hdjay0129 - Can you post the full value of payload?

Here’s the full payload that I’m trying to send via Postman for testing this out -

"{\"s_time\": 123456, \"reset\" : false}"

inside body as raw data with content type application/json

It seems as if nakama is expecting a json object from the client but it’s receiving a string.

How are you sending the payload? Please post the unity code that sends the payload to make sure.

@asheraryam I really don’t have any problem with calling this RPC in unity or via direct curl either.
I just want to know how to properly send payload to nakama RPC via Postman. Like what format for payload does nakama expects?

Alright, finally got it working.
I was using GET method, changed it to POST and it worked.
Here’s the payload format that Nakama expects just in case someone gets to this issue -
"{\"s_time\": 123456, \"reset\" : false}"

1 Like

@hdjay0129 This has been improved on master (20d6ab3c) and will be part of the next release of Nakama. It will allow you to pass a new query parameter to the RPC endpoint for server to server function calls called unwrap which allows for raw JSON input and output. The technical details are that it allows us to bypass the GRPC service definition which doesn’t understand JSON as a direct input.

That’s a good news @novabyte.

1 Like