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

I have an application (IPControl) that uses Soap/WSDL for an API. In order to access that API I first have to send an "init" message and from the response to that, get the session ID out of the response. That session ID is needed for future message for that request.

To make it even more complicated, it uses a "multiref" response with other stuff in the response, rather than just "sesionid=xxx"

Here's an example of a response to the initial message:

<?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/e +nvelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="htt +p://www.w3.org/2001/XMLSchema-instance"> <soapenv:Header> <ns1:sessionID soapenv:actor="http://schemas.xmlsoap.org/s +oap/actor/next" soapenv:mustUnderstand="0" href="#id0" xmlns:ns1="htt +p://xml.apache.org/axis/session"/> </soapenv:Header> <soapenv:Body> <ns2:initExportDeviceResponse soapenv:encodingStyle="http: +//schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://diamondip.com +/netcontrol/ws/"> <initExportDeviceReturn href="#id1"/> </ns2:initExportDeviceResponse> <multiRef id="id0" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:long" x +mlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">-91025392591 +2285490</multiRef> <multiRef id="id1" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:WSContext" +xmlns:ns3="http://service.ipcontrol.diamondip.com" xmlns:soapenc="htt +p://schemas.xmlsoap.org/soap/encoding/"> <contextId xsi:type="soapenc:string">ContextId_1364228 +178892</contextId> <contextType xsi:type="soapenc:string">Export_Device</ +contextType> <filter xsi:type="soapenc:string">DeviceType='NAS'</fi +lter> <firstResultPos href="#id2"/> <internalResultCount href="#id3"/> <maxResults href="#id4"/> <options xsi:type="soapenc:string" xsi:nil="true"/> <query xsi:type="soapenc:string">SELECT DISTINCT huge- +ass sql statement cropped for clarity) ) ) ) ) ORDER BY 1</query> <resultCount href="#id5"/> </multiRef> <multiRef id="id4" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:int" xmlns: +soapenc="http://schemas.xmlsoap.org/soap/encoding/">5000</multiRef> <multiRef id="id3" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:int" xmlns: +soapenc="http://schemas.xmlsoap.org/soap/encoding/">0</multiRef> <multiRef id="id5" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:int" xmlns: +soapenc="http://schemas.xmlsoap.org/soap/encoding/">152</multiRef> <multiRef id="id2" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:int" xmlns: +soapenc="http://schemas.xmlsoap.org/soap/encoding/">0</multiRef> </soapenv:Body> </soapenv:Envelope>

Most of that is junk for verification, but I included in case it adds complications. The only thing I really want to pull out that message is the "ns1:sessionID" which has the value of multiRef id="id0"

Here's what I have for a bare-minimum script right now:

## USE THESE use strict; use warnings; use SOAP::Lite; use Data::Dumper; ## AUTHENTICATION my $user = 'username'; my $pass = 'password'; sub SOAP::Transport::HTTP::Client::get_basic_credentials { return $use +r => $pass; } ## WSDL AND FILTER DEFINIATION my $WSDL = 'http://localhost:8080/inc-ws/services/Exports?wsdl'; my $filter = "DeviceType='NAS'"; ## MAKE THE CALL, PRINT THE RESULTS my $client = SOAP::Lite ->service($WSDL); my $result = $client->initExportDevice($filter); print Dumper($result);
I get the results just fine (filter, resultcount, maxresults, etc) when I print the $result. Just not sure how to get access to that value that was in the header.

Replies are listed 'Best First'.
Re: SOAP::Lite Client - Read response header
by Anonymous Monk on Mar 26, 2013 at 02:00 UTC
Re: SOAP::Lite Client - Read response header
by McA (Priest) on Mar 26, 2013 at 10:05 UTC

    Hi,

    I had to smile seeing that Anonymous Monk gave a snippet hopefully extracting information from Body while you asked for Header, but probably he wanted to test your transfer capabilities.

    Here is my snippet in the hope it helps you:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use local::lib 'lib1'; use SOAP::Lite; my $xml = q{<?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/e +nvelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="htt +p://www.w3.org/2001/XMLSchema-instance"> <soapenv:Header> <ns1:sessionID soapenv:actor="http://schemas.xmlsoap.org/s +oap/actor/next" soapenv:mustUnderstand="0" href="#id0" xmlns:ns1="htt +p://xml.apache.org/axis/session"/> </soapenv:Header> <soapenv:Body> <ns2:initExportDeviceResponse soapenv:encodingStyle="http: +//schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://diamondip.com +/netcontrol/ws/"> <initExportDeviceReturn href="#id1"/> </ns2:initExportDeviceResponse> <multiRef id="id0" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:long" x +mlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">-91025392591 +2285490</multiRef> <multiRef id="id1" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:WSContext" +xmlns:ns3="http://service.ipcontrol.diamondip.com" xmlns:soapenc="htt +p://schemas.xmlsoap.org/soap/encoding/"> <contextId xsi:type="soapenc:string">ContextId_1364228 +178892</contextId> <contextType xsi:type="soapenc:string">Export_Device</ +contextType> <filter xsi:type="soapenc:string">DeviceType='NAS'</fi +lter> <firstResultPos href="#id2"/> <internalResultCount href="#id3"/> <maxResults href="#id4"/> <options xsi:type="soapenc:string" xsi:nil="true"/> <query xsi:type="soapenc:string">SELECT DISTINCT huge- +ass sql statement cropped for clarity) ) ) ) ) ORDER BY 1</query> <resultCount href="#id5"/> </multiRef> <multiRef id="id4" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:int" xmlns: +soapenc="http://schemas.xmlsoap.org/soap/encoding/">5000</multiRef> <multiRef id="id3" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:int" xmlns: +soapenc="http://schemas.xmlsoap.org/soap/encoding/">0</multiRef> <multiRef id="id5" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:int" xmlns: +soapenc="http://schemas.xmlsoap.org/soap/encoding/">152</multiRef> <multiRef id="id2" soapenc:root="0" soapenv:encodingStyle= +"http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:int" xmlns: +soapenc="http://schemas.xmlsoap.org/soap/encoding/">0</multiRef> </soapenv:Body> </soapenv:Envelope> }; my $som = SOAP::Deserializer->deserialize($xml); my $subsom = $som->match('/Envelope/Header/[1]'); my $res = $subsom->valueof(); print "Result: $res\n";

    McA

      That definitely works if I can get at the raw XML response from the server. Which is what I posted and you used for $xml.

      However, I should have been a bit clearer in my initial post. The XML response I posted above was captured via a tcpdump. The problem I'm getting is that when I use SOAP::Lite and print the results via Dumper, I'm only seeing the value in the body. So it looks like SOAP::Lite may be stripping the header off before I can get access to it.

      Using the same code as from the first post, this is the what I see from the dumper printout

      $VAR1 = bless( { 'resultCount' => '152', 'options' => undef, 'query' => 'SELECT DISTINCT huge-ass sql statement', 'filter' => 'DeviceType=\'NAS\'', 'maxResults' => '5000', 'firstResultPos' => '0', 'contextId' => 'ContextId_1364302533581', 'contextType' => 'Export_Device', 'internalResultCount' => '0' }, 'WSContext' );

      So, I'm thinking there needs to be something changed in these two lines in order to read the header (Or else, I need to do this with some completely different method) - just not sure what

      ## MAKE THE CALL, PRINT THE RESULTS my $client = SOAP::Lite ->service($WSDL); my $result = $client->initExportDevice($filter);

        Hi,

        I don't have the possibility to test this. But as far as I see in the docs $result should be a SOAP::SOM and you should be able to call the methods ob this class. There are three intersting ones: root, envelope, header.

        Sorry, I can't prove it.

        McA