3

I am having trouble getting curl and Json (Node.js) to work when I in curl omit the "[ and ]" part of the JSON-RPC request params. I am using named parameters (i.e. an object, not an array).

This works:

curl -v -i POST -H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method":"registerPerson", "params": ["{\"username\":\"morten10\",\"password\":\"mypass\"}"], "id":1 }' \
http://localhost:3000

However, when I omit the [" and ]" from the params it doesn't work:

curl -v -i POST -H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method":"registerPerson", "params": {\"username\":\"morten10\",\"password\":\"mypass\"}, "id":1 }' \
http://localhost:3000

and Jayson gives this error message:

TypeError: First argument must be a string or Buffer

However, according to the JSON-RPC 2.0 specification and its examples, I should be able to exclude the "[ and ]" part from params when I use named parameters, as in this example:

--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
<-- {"jsonrpc": "2.0", "result": 19, "id": 4}

What am I not getting?

Thanks!

1 Answer 1

2

I think your object needs to be a string as suggested by the error.

Try:

curl -v -i POST -H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method":"registerPerson", "params": "{\"username\":\"morten10\",\"password\":\"mypass\"}", "id":1 }' \
http://localhost:3000
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but I tried that already. But changing to: curl -v -i POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method":"registerPerson", "params": "{\"username\":\"morten10\",\"password\":\"mypass\"}", "id":1 }' http://localhost:3000 makes Jayson give this message (before my code is reached): {"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid request"}} Go figure.
It is interesting to note that Jayson sets the "id" to null in that error response. According to the JSON-RPC 2.0 specification the "id" must be null only if there is an error detecting the "id" in the request. So, apparently Jayson cannot even locate the "id" in the request. Weird. Assuming Jayson is not buggy, I am guessing it is a quote-escape issue somehow, but I fail to see what is wrong in that request.
the only other thing I could suggest is putting it through a linter to see if that will help you figure it out, I notice removing the outer single quotes will allow it to pass on jsonlint.com for example.
yes, it should work, also according to the jsonlint.com. I have now filed a bug report with Jayson on GitHub, here: github.com/tedeh/jayson/issues/87. Thanks for the help, Brian :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.