neonxp/jsonrpc2

support requests without params

Closed this issue · 4 comments

Hi, is there any way to add handlers for methods that don't require params?

I implemented the following handler:

r.Register("test", rpc.H(testHandler))

func testHandler(ctx context.Context, args *any) (int, error) {
	return 0, nil
}

A request without sending empty parameteres returns an Parse error.

$ curl -X POST \
        http://localhost:8080 \
        -H 'Content-Type: application/json' \
        -d '{
          "jsonrpc": "2.0",
          "id": "0",
          "method": "test",
  }'

 {"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":"0"}

Sending empty args seems to work but that's not very user friendly.

$ curl -X POST \
        http://localhost:8080 \
        -H 'Content-Type: application/json' \
        -d '{
          "jsonrpc": "2.0",
          "id": "0",
          "method": "test",
          "params": {}
  }'
  
{"jsonrpc":"2.0","result":0,"id":"0"}

Hello! Thank you for contribution! But what about alternative way without another wrapper: #4 ?

I have no preference for any of the options. One option may confuse the user with an additional wrapper. Another option with an empty pointer looks like a hack. I honestly don't know what would be the best solution.

My thoughts were that the function signature would be clearer with a second wrapper but your alternative certainly works too. I don't know what the best solution is either but I personally would prefer a second wrapper so you don't have to write functions with useless parameters for the sake of simplicity

Good point. So I will decline my PR. I have to ask you to add information about the new wrapper to the readme and add an example to the readme code before I merge it.