Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Setting XML prefix for tags with SOAP::Lite

by TheFluffyOne (Beadle)
on Jul 30, 2008 at 18:39 UTC ( [id://701217]=perlquestion: print w/replies, xml ) Need Help??

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

I'm currently playing around with accessing a WSDL service (RealOps AMP/BMC RBA for those interested) using SOAP::Lite and need to craft a request in the right format.

I managed to find an old post that almost did what I needed and munged the code into the following:

#!/usr/bin/perl -w use strict; use SOAP::Lite +trace => 'debug', maptype => { }; my $userId = "admin"; my $password = "adminpw"; my $wsdl = "http://10.50.50.132:9080/ws/workflow"; my $gridName = SOAP::Header->name("grid-name" => SOAP::Data->type('' = +> 'SampleGrid1')); my $rba = SOAP::Lite->uri('urn:realops.com:amp:workflow')->proxy($wsdl +); my $wsse = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-ws +security-secext-1.0.xsd"; my $securityHeader = SOAP::Header->name( Security => { UsernameToken => { Username => SOAP::Data->type('' => $userId)->prefix('wsse' +), Password => SOAP::Data->type('' => $password)->prefix('wss +e'), } })->uri($wsse)->prefix('wsse'); my $result = $rba->getAllSOAPServices($gridName, $securityHeader)

This code results in a soap header as follows:

<soap:Header> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/ +oasis-200401-wss-wssecurity-secext-1.0.xsd"> <UsernameToken> <wsse:Password>adminpw</wsse:Password> <wsse:Username>admin</wsse:Username> </UsernameToken> </wsse:Security> <grid-name>SampleGrid1</grid-name> </soap:Header>

The header needs to look like this:

<soapenv:Header> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/ +oasis-200401-wss-wssecurity-secext-1.0.xsd"> <wsse:UsernameToken> <wsse:Username>admin</wsse:Username> <wsse:Password>adminpw</wsse:Password> </wsse:UsernameToken> </wsse:Security> <grid-name xmlns="urn:realops.com:amp:workflow">SampleGrid1</grid- +name> </soapenv:Header>

How can I get the "wsse" prefix on the UsernameToken tag? I also need to add the xmlns attribute to grid-name, but I haven't finished RTFMing on that one yet...

Replies are listed 'Best First'.
Re: Setting XML prefix for tags with SOAP::Lite
by AltBlue (Chaplain) on Jul 31, 2008 at 00:53 UTC
    How can I get the "wsse" prefix on the UsernameToken tag?
    my $securityHeader = SOAP::Header->new( name => 'Security', uri => $wsse, prefix => 'wsse', value => \SOAP::Data->new( name => 'UsernameToken', prefix => 'wsse', value => { Username => SOAP::Data->type( '' => $userId )->prefix('wss +e'), Password => SOAP::Data->type( '' => $password )->prefix('w +sse'), } ) );

    Update: sorry, missed your second issue...

    I also need to add the xmlns attribute to grid-name
    my $gridName = SOAP::Header->new( name => 'grid-name', value => 'SampleGrid1', uri => 'urn:realops.com:amp:workflow', prefix => '', type => '', );
    'HTH

      Fantastic, thanks Altblue. That's exactly what I needed.

      With the SOAP call in better shape, I had to work around an issue with the Username and Password being in the wrong order (the WSDL specifies them as a sequence).

      I think I just have one more hurdle to overcome. The current version of the script results in the SOAP body:

      <soap:Body> <DirList xmlns="Test_Module" xsi:nil="true" /> </soap:Body>

      But in order to work it seems that it should be:

      <soap:Body> <tra:DirList /> </soap:Body>

      (Where tra is a namespace defined as "Test_Module"). Additionally, I'll want to add a couple of parameters like so:

      <soap:Body> <tra:DirList> <param1>value1</param1> <param2>value2</param2> </tra:DirList> </soap:Body>

      My recent attempts at adding the parameters have resulted in lots of c-gensym tags added, which I think is an indication that I'm not referencing the data correctly

      The code as it currently stands (without the attempt at adding parameters):

      #!/usr/bin/perl -w use strict; use SOAP::Lite +trace => 'debug', maptype => { }; use Tie::IxHash; my $userId = "admin"; my $password = "adminpw"; my %authHash; tie %authHash, "Tie::IxHash"; %authHash = ( Username => SOAP::Data->type( '' => $userId )->prefix('wss +e'), Password => SOAP::Data->type( '' => $password )->prefix('w +sse'), ); my $wsdl = "http://10.50.50.132:9080/ws/workflow"; my $rba = SOAP::Lite->uri('Test_Module')->proxy($wsdl); $rba->on_action(sub { return '"execute-workflow"'; } ); my $wsse = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-ws +security-secext-1.0.xsd"; my $gridName = SOAP::Header->new( name => 'grid-name', value => 'Amsterdam', uri => 'urn:realops.com:amp:workflow', prefix => '', type => '', ); my $securityHeader = SOAP::Header->new( name => 'Security', uri => $wsse, prefix => 'wsse', value => \SOAP::Data->new( name => 'UsernameToken', prefix => 'wsse', value => \%authHash ) ); my $result = $rba->DirList($gridName, $securityHeader)->result(); print "\n\n\n$result\n";

        Well as I was cleaning up my parameter-adding code to post, I had some inspiration and have found a way to do it (though it seems clunky). I have also realised that it wasn't the lack of prefix causing the problem, it was that I was using the wrong method name (d'oh!). New code:

        my @params = ( SOAP::Data->new( prefix => '', name => 'param1', value => SOAP::Data->type('' =>'value1') ), SOAP::Data->new( prefix => '', name => 'param2', value => SOAP::Data->type('' =>'value2') ) ); my $result = $rba->DirList(@params, $gridName, $securityHeader)->resul +t();

        Which results in:

        <soap:Body> <DirList xmlns="Test_Module"> <param1>value1</param1> <param2>value2</param2> </DirList> </soap:Body>

        The tag "DirList" needs to be "DirList-Request", but I can't use this directly in my current code as I get bareword errors...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://701217]
Approved by cmv
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found