Search code examples
ruby-on-railsdatetimeparametersxml-rpctrac

XMLRPC::DateTime Formatting


I'm working on a Rails project that requires using an XMLRPC protocol to access a Trac database.

Everything had been working fine until I needed to make a request with a XMLRPC::DateTime parameter. Now when I go to load the page, I get the error message:

Wrong content-type (received 'application/xml' but expected 'text/xml')

I have tried formatting the date parameter in a bunch of different ways (listed below), but nothing seems to be working.

I've tried:

    - @date = Time.now #=> (Tue Jul 10 10:18:01 -0700 2012)
    - @date = DateTime.now #=> (2012-07-10T10:17:37-07:00)
    - @date = Time.now.to_datetime #=> (2012-07-10T10:19:24-07:00)
    - @date = XMLRPC::Convert.dateTime(Time.now.to_datetime.to_s) #=> 
      #<XMLRPC::DateTime:0x1276aa068> 
      (The above hex number changes each time the page is refreshed)

Do you know what could be going wrong here?

UPDATE: The code where I make the call is pasted below (I'm using the trac4r ruby gem to actually make calls to the API):

def foo  
   date = DateTime.now.to_s  
   tickets = trac.tickets.query(:max => 1000, :created_at => date)  
   return tickets.size  
end

Solution

  • The gem you're using hasn't been updated in more than 2 years. I mention this because with age like that it is possible that some features of the gem no longer work due to changes that may have occurred in Ruby's XMLRPC since (I will have to dig more to find proof of this).

    What I don't like is this args_to_trac_args method.

    > args_to_trac_args({:max => 1000, :created_at => DateTime.now.to_s})
      => "max=1000&created_at=2012-07-10T13:58:13-04:00"
    

    This string is then passed to an XMLRPC::Client instance's call method.

    In the XMLRPC::Client's call method, the above string is passed to methodCall in XMLRPC::Create. If you view the source of that method, you'll see it tries to generate valid XML elements for each parameter passed.

    parameter = params.collect do |param|
        @writer.ele("param", conv2value(param))
    end
    

    This is generating a single <param> element like

    <param>
      <value>
        <string>max=1000&amp;created_at=2012-07-10T13:58:13-04:00</string>
      </value>
    </param>
    

    If I generate a full document by hand using the "max=1000&created_at=2012-07-10T13:58:13-04:00" string as the gem will

    require "xmlrpc/client"
    
    writer = XMLRPC::Create.new
    writer.methodCall('ticket.query', "max=1000&created_at=2012-07-10T13:58:13-04:00")
    

    the XML generated is invalid due to the &created_at=...

    <?xml version="1.0" ?>
    <methodCall>
        <methodName>ticket.query</methodName>
        <params>
            <param>
                <value>
                    <string>max=1000&amp;created_at=2012-07-10T13:58:13-04:00</string>
                </value>
            </param>
        </params>
    </methodCall>
    

    To me it seems the intended response is something more like

    <?xml version="1.0" ?>
    <methodCall>
        <methodName>ticket.query</methodName>
        <params>
            <param>
                <value>
                    <string>max=1000</string>
                </value>
            </param>
            <param>
                <value>
                    <string>created_at=2012-07-10T13:58:13-04:00</string>
                </value>
            </param>
        </params>
    </methodCall>
    

    but I don't know enough about what Trac is expecting as request paramaters, or how the gem was intending for Ruby's XMLRPC client to work 2+ years ago.

    I would verify the expected request format (one XML <param> containing a querystring of arguments, or multiple <param> elements), fork the Github repo, and make the necessary changes to args_to_trac_args (to me that seems to be the source of the problem).

    If the params are not joined into a single string and left as an array like ["max=1000", "created_at=2012-08-10T13:58:13-04:00"],methodCall` will generate the valid XML in the 2nd document shown above

    writer = XMLRPC::Create.new
    writer.methodCall('ticket.query', "max=1000", "created_at=2012-07-10T13:58:13-04:00")