http://www.perlmonks.org?node_id=961197

zeltus has asked for the wisdom of the Perl Monks concerning the following question:

I am fairly new to XML-RPC stuff and I think I might be approaching it from an "unusual" angle! :-). Anyway, I have an API Server I need to interface with that expects to see input along the lines of

<?xml version="1.0"?> <methodCall> <methodName>Gateway.SendMessage</methodName> <params> <param> <value> <struct> <member> <name>Service</name> <value><int>0</int></value> </member> <member> <name>Password</name> <value>aPassword</value> </member> <member> <name>Text</name> <value>A demonstration short message</value> </member> <member> <name>DeliveryTime</name> <value><dateTime.iso8601>20050421T09:32:00</dateTime.iso8601>< +/value> </member> </struct> </value> </param> </params> </methodCall>

Now,

use RPC::XML::Client; my $client = RPC::XML::Client->new('http://path-to-rpc-server:19000'); my $res = $client->send_request('Gateway.SendMessage');

seems (I hope) clear enough, but I can't for the life of me understand how to get the Service,Password,Text etc parameters across.

Please, please can someone help?

Since I am coming across more and more API's written this way, it feels natural to take their expected XML input and translate it into what RPC::XML can work with. But I can't find any examples that display this sort of thinking. Am I really coming at this backwards?

Thanks

Bill

Replies are listed 'Best First'.
Re: RPC::XML::Client example needed
by Khen1950fx (Canon) on Mar 23, 2012 at 12:29 UTC
    This is untested, but, in general, I would attack the problem like this:
    #!/usr/bin/perl -l use strict; use warnings; use RPC::XML::Client; use Devel::SimpleTrace; use Data::Dumper::Concise; my $client = RPC::XML::Client->new('http://path-to-rpc-server:19000'); my $req = RPC::XML::Client->new( 'Gateway.SendMessage', 'show', 'channels', ); $client->credentials(qw(Gateway user password)); my $resp = $client->send_request($req); print Dumper( ref $resp ? join(', ', @{$resp->value}) : "Error: $resp" );
Re: RPC::XML::Client example needed
by trwww (Priest) on Mar 23, 2012 at 20:56 UTC

    Based on that method signature, it should be as simple as:

    my $res = $client->send_request('Gateway.SendMessage' => { Service => 0, Password => 'apassword', Text => 'A demonstration short message', DeliveryTime => '20050421T09:32:00', });

    Sometimes RPC::XML doesn't serialize the data to the correct data type (you can tell by stringifying the object or watching the raw network data). In this case you have to wrap the value you want to send around an RPC::XML Data Class instance. There is an example of this right in the synopsis where you can see RPC::XML::int is being used to explicitly set the data type.