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

burningdog’s picture

Ah, 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:

  $data = array(
    'method' => '"my_module.hello"',
    'params' => array(
      'name' => '"'. $name .'"',
      'mail' => '"'. $mail .'"',
    )
  );

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:

{
    "method"  : "my_module.hello",
    "params"  : { "name" : Dries, "b" : me@example.com }
}

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?

Hugo Wetterberg’s picture

Hi Roger,
Curl doesn't re-format the request body. What you should do is to set:

curl_setopt($ch, CURLOPT_HTTPHEADER , array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

Try that and see if it works

Cheers,
Hugo

solipsist’s picture

This works:

<?php
  // prepares the request
  $request = array(
    'version' => "1.1",
    'method' => $method,
    'params' => json_encode($params),
    'id' => $id
  );

  // Execute JSON request
  $ch = curl_init();

  // Set options
  curl_setopt($ch, CURLOPT_URL, $this->url);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  if ($response = @curl_exec($ch)) {
    $response = json_decode($response, true);
  }
?>
sunset_bill’s picture

Greetings,

I'm running into the same thing, though I'm hitting my JSONRPC server with a prototype.js Ajax.Request(), like so

  var params = {method: "node.get", nid: 47};

  new Ajax.Request('http://example.com/services/json-rpc', {
    method: 'post',
    parameters: params,
    onSuccess: function(transport){
      var json = transport.responseText.evalJSON();
alert(json);
      // OK, that works, now do actual useful stuff
    }
  });

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:

 curl --data 'method="node.get"' --data 'nid=47' http://localhost/drupal6/services/json-rpc
{"error":{"name":"JSONRPCError","code":-32700,"message":"No parameters received, the likely reason is malformed json, the method 'node.get' has required parameters."},"version":"1.1"}

So close and yet so far away! Any ideas?

thanks,
SB

sunset_bill’s picture

More 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

Array
(
    [method] => node.get
    [nid] => 47
    [params] => 
)
{"error":{"name":"JSONRPCError","code":-32700,"message":"No parameters received, the likely reason is malformed json, the method 'node.get' has required parameters."},"version":"1.1"}

which tells me my problem is in

var send = {method: "node.get", nid: 47}; // changed the var name to avoid confusion

but I'm just a middling javascripter and can't figure out how to get nid into params.

Hugo Wetterberg’s picture

Take 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.

sunset_bill’s picture

OK, 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

var send = {method: "node.get", params: {nid: 47}}

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:

Array
(
    [method] => node.get
    [params] => 
)
{"error":{"name":"JSONRPCError","code":-32700,"message":"No parameters received, the likely reason is malformed json, the method 'node.get' has required parameters."},"version":"1.1"}

When I JSON-encode my send variable, I get

Array
(
    [params] => 
)
{"error":{"name":"JSONRPCError","code":-32600,"message":"The received JSON not a valid JSON-RPC Request"},"version":"1.1"}

Any other hints?

thanks,
SB