bitboxer / jimson

JSON-RPC 2.0 client and server for Ruby

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

params passed as an array while a json object is expected

snitko opened this issue · comments

So, I've been playing with a Counterparty json-rpc server (for reference, don't look too hard at it: http://counterpartyd.readthedocs.org/en/latest/API.html) and it returned me an error everytime I tried to make a request with params saying the following:

{ "message": "Invalid Request", "data": "Arguments must be passed as a JSON object (list of unnamed arguments not supported)" }

I digged deeper into your code and found the issue here:

module Jimson
  class ClientHelper

    def send_single_request(method, args)
      namespaced_method = @namespace.nil? ? method : "#@namespace#{method}"
      post_data = MultiJson.encode({
        'jsonrpc' => JSON_RPC_VERSION,
        'method'  => namespaced_method,
        'params'  => args.try(:first), # <= PROBLEM WAS HERE: it was just `args`
        'id'      => self.class.make_id
      })
      resp = RestClient.post(@url, post_data, @opts)
      if resp.nil? || resp.body.nil? || resp.body.empty?
        raise Client::Error::InvalidResponse.new
      end

      return resp.body
    end

  end

end

So it seems the server I'm interacting with expects params to be a json object and not an array. The fix I applied - using the first element of the array - works, however I'm not sure whether this is something specific to the Counterparty RPC server or maybe it's jimson implementation deficiency. Please take a look at it, you might want to decide to give users more freedom as to how to pass params.

Same issue here. According to http://www.jsonrpc.org/specification#parameter_structures the request object needs to hold parameters in a object, with { parameter_name => value } or as an array in the order the server expects the keys to be.

4.2 Parameter Structures
If present, parameters for the rpc call MUST be provided as a Structured value. Either by-position through an Array or by-name through an Object.

  • by-position: params MUST be an Array, containing the values in the Server expected order.
  • by-name: params MUST be an Object, with member names that match the Server expected parameter names. The absence of expected names MAY result in an error being generated. The names MUST match exactly, including case, to the method's expected parameters.