I'm posting JSON data to the JSON-RPC server following the convention at http://drupal.org/node/305799, like so:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, 'http://example.com/services/json-rpc');
//prepare the field values being posted to the service
$data = array(
'method' => '"my_module.hello"',
'name' => '"'. $name .'"',
'mail' => '"'. $mail .'"',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//make the request
$result = curl_exec($ch);
However, whenever I post it I get this response:
{"error":{"name":"JSONRPCError","code":-32602,"message":"Argument 'name' is required but was not recieved"},"version":"1.1"}
I'm not passing any parameters through, only arguments, so I'm wondering if there's a logical problem on line 61 of JsonRpcServer.php, where it does this check: if (empty($this->params)) {, and if so, returns an error (my params are always empty).
In any case, the block of code starting on line 80 ("Map parameters to arguments") isn't getting the value of the arguments. Is there anything I need to change with the way I'm sending arguments through?
Comments
Comment #1
burningdog commentedAh, reading the spec at http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html shows I need to pass through an array called 'params' - a "Procedural Call with Named Parameters". I changed my code to:
but still got this message:
No parameters recieved, the likely reason is malformed json, the method 'my_module.hello' has required parameters.(and yes, $name and $mail do have values).The parameters are supposed to be passed through like so:
How do I see exactly how curl is re-formatting them when it's making the POST? I listened in via tcpdump but all I got was a "Content-Type: multipart/form-data;" I'm probably formatting the array incorrectly - what's the correct way?
Comment #2
Hugo Wetterberg commentedHi Roger,
Curl doesn't re-format the request body. What you should do is to set:
Try that and see if it works
Cheers,
Hugo
Comment #3
solipsist commentedThis works:
Comment #4
sunset_bill commentedGreetings,
I'm running into the same thing, though I'm hitting my JSONRPC server with a prototype.js Ajax.Request(), like so
My onSuccess is happening, but the response I'm getting (via Firebug) is
"No parameters received, the likely reason is malformed json, the method 'node.get' has required parameters."However, Firebug shows two parameters being passed, the node.get method and an nid of 47.
I've tried running my params through prototype's toJSON() function, but that just gets me
"The received JSON not a valid JSON-RPC Request"I also get the same thing when I try curl:
So close and yet so far away! Any ideas?
thanks,
SB
Comment #5
sunset_bill commentedMore clues. I put a print_r in the module code to put the value of $in into the error log, and it gives me this
which tells me my problem is in
but I'm just a middling javascripter and can't figure out how to get nid into params.
Comment #6
Hugo Wetterberg commentedTake a look at how jsonrpc_server calls itself when using the helper function:
https://github.com/hugowetterberg/jsonrpc_server/blob/master/jsonrpc_ser...
And, when in doubt, read the spec:
JSON-RPC 1.1 draft: http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
JSON-RPC 2.0 proposal: http://groups.google.com/group/json-rpc/web/json-rpc-1-2-proposal
jsonrpc_server aims to be spec-compliant for both 1.1 and 2.0, if you find a case where it's not - please file a issue.
Comment #7
sunset_bill commentedOK, I've read the spec and it says parameters need to be passed in an array called 'params'. So, I changed the variable I'm passing into the parameters element to be
which looks to me like params should have one element, nid, with the value of 47. Looking at it in Firebug, with a print_r statement to show the value of $in:
When I JSON-encode my send variable, I get
Any other hints?
thanks,
SB