Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Getting an XML-RPC array with Frontier::Client

by jaldhar (Vicar)
on Apr 01, 2003 at 15:50 UTC ( #247264=perlquestion: print w/replies, xml ) Need Help??
jaldhar has asked for the wisdom of the Perl Monks concerning the following question:

i'm using Frontier::Client to get back information from Advogato's XML-RPC interface and I'm having trouble with the methods that return an array. Here is a sample invocation of the test.guess method:

---- request ---- <?xml version="1.0"?> <methodCall> <methodName>test.guess</methodName> <params> </params> </methodCall> ---- response ---- <?xml version="1.0"?> <methodResponse> <params> <param> <array> <data> <value> <string>You guessed</string> </value> <value> <int>42</int> </value> </data> </array> </param> </params> </methodResponse>
I try to access it from perl like this:
my $client = Frontier::Client->new( 'url' => '', use_objects => 1, debug => 1,); my ($string, $int) = $client->call('test.guess') or die "$!\n";
and get back the XML above plus:
wanted `value' or `name' tag, got `array' at line 5 column 6
and the variables don't get filled in. Is this a limitation in the module or did I do something wrong?


Replies are listed 'Best First'.
Re: Getting an XML-RPC array with Frontier::Client
by tall_man (Parson) on Apr 01, 2003 at 19:38 UTC
    It seems the Frontier::Client only expects to return simple values, not arrays. You'll have to go down to the Frontier::RPC2 level and do "decode" calls for yourself. Here's an example:
    sub doCommand { # Decode the results of the raw command from XML. my ($uri, $coder, @rest) = @_; my $encoded = doRawCommand($uri, $coder, @rest); return $encoded if ($encoded eq ""); my $result; # Decode XML RPC wrapper case. my $decoded = $coder->decode($encoded); # Note: the result is an array ref with params inside $result = $decoded->{value}[0]; return $result; }

    Update: I should explain that doRawCommand is a routine I wrote to encode and make an XML-RPC request, and that $coder is an instance of Frontier::RPC2.

    Update2: One thing about your original code is that Frontier::Client::call will never return an array of values, even if the decoding had worked. It should be able to do an array reference, though.

    Update3: I just tried an encoding example, trying to match what Advogato returns:

    use strict; use Frontier::RPC2; my $coder = Frontier::RPC2->new(use_objects => 1); my $result = [ "you guessed", 42 ]; my $xml_string = $coder->encode_response($result); print $xml_string,"\n";
    This is the result I got:
    <?xml version="1.0"?> <methodResponse> <params> <param><value><array><data> <value><string>you guessed</string></value><value><i4>42</i4></value>< +/data></array></value> </param> </params> </methodResponse>
    The difference is a "value" tag around the "array" level. I believe that is what is throwing off your decode. Advogato must be using a different version of the encoding scheme.

    I would suggest asking their support board. If they can't help, you could use a wrapper like my subroutine above and munge the return value to the right syntax before decoding it.

      Update: nicer error-handling code, correction of syntax errors.

      Sorry I got sidetracked by other things (or rather I got retracked from this side track :-) but I returned to this subject and your advice was invaluable.

      In the end though I used RPC::XML which is just as easy to use and apparently more complete. Here is what my example code would look like with this module:

      use RPC::XML::Client; my $client = RPC::XML::Client->new(''); my $result = $client->send_request('test.guess'); die $result unless defined($result); die $result->code . ': ' . $result->string if $result->is_fault; print join(' ', @{$result->value}), "\n";


      Dude, I found a real cool solution.. Just pass the array elements like below $result1 = $server->call('method1',reasonsArray =>"reason1","reason2","reason3") Thanks, Sarat
        you have to keep all your array elemement in square braces. in the above comment square braces were removed by this site tagging process