Consuming .Net Web Service using Perl and SOAP Lite - perl

I'm trying to consume a .Net Web Service using perl and SOAP Lite.
When I consume the web service in a .Net client - it posts to the .asmx endpoint the following:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tns="http://mysoapnamespace.com/"
xmlns:types="http://mysoapnamespace.com/encodedTypes">
<soap:Body>
<tns:HellowWorld />
</soap:Body>
</soap:Envelope>
How can I generate that same request using SOAP Lite? I've been through a variety of SOAP Lite docs and articles with no luck. So far I have the following:
#!/usr/bin/perl
use SOAP::Lite 'trace', 'debug' ;
$api_ns = "https://mysoapnamespace.com";
$api_url = "http://mysoapnamespace/api.asmx";
$action = "HelloWorld";
my $soap = SOAP::Lite
-> readable(1)
-> uri($api_ns)
-> proxy($api_url)
-> on_action(sub { return "\"$action\"" }) ;
return $soap->HellowWorld();
This generates this, incorrect XML:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HellowWorld xmlns="http://mysoapnamespace.com" xsi:nil="true" />
</soap:Body>
</soap:Envelope>
Update:
When I post the 1st xml to my service using fiddler it returns my "Hello World" result. When I post the 2nd I get the following:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server was unable to read request. ---> System.InvalidOperationException: There is an error in XML document (9, 6). ---> System.InvalidOperationException: <HellowWorld xmlns='http://mysoapnamespace.com'> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read21_HellowWorld()
at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer28.Deserialize(XmlSerializationReader reader)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
--- End of inner exception stack trace ---
at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>

Found the problem - the trailing slash on the namespace. .Net just figures that stuff out for your but it needs to be explicitly set in Perl. Also, figured out how to us the ns() function to add names spaces.
This will generate the correct XML.
#!/usr/bin/perl
use SOAP::Lite 'trace', 'debug' ;
$api_ns = "https://mysoapnamespace.com/";
$api_url = "http://mysoapnamespace/api.asmx";
$action = "HelloWorld";
my $soap = SOAP::Lite
-> readable(1)
-> ns($api_types,'types')
-> ns($api_ns,'tns')
-> proxy($api_url)
-> on_action(sub { return "\"$action\"" }) ;
return $soap->HellowWorld();
This link was very helpful in figuring out SOAP::Lite - http://kobesearch.cpan.org/htdocs/SOAP-Lite/SOAP/Lite.pm.html

I had to do the following to get it to work (add this line after my $soap... line):
$soap->ns('http://schemas.xmlsoap.org/soap/envelope/',"s");
I hope this will save someone some time... It took a while to get it figured out... :-)
BTW: I am using .Net 4.5 WCF with a windows service for Web Service server and Perl (activestate) V5.16.3 with SOAP::Lite V 1.08.

Related

Netsuite get list of files inside a folder using soap webservice

I am actually trying to get a list of files(or their internalId's) inside a folder using soap webservice from netsuite file cabinet
I am using the following request to make a search request
<soapenv:Header>
<urn:tokenPassport>
<urn1:account xmlns:urn1="urn:core_2020_2.platform.webservices.netsuite.com">*****</urn1:account>
<urn1:consumerKey xmlns:urn1="urn:core_2020_2.platform.webservices.netsuite.com">*****</urn1:consumerKey>
<urn1:token xmlns:urn1="urn:core_2020_2.platform.webservices.netsuite.com">*****</urn1:token>
<urn1:nonce xmlns:urn1="urn:core_2020_2.platform.webservices.netsuite.com">*****</urn1:nonce>
<urn1:timestamp xmlns:urn1="urn:core_2020_2.platform.webservices.netsuite.com">*****</urn1:timestamp>
<urn1:signature algorithm="HMAC-SHA256" xmlns:urn1="urn:core_2020_2.platform.webservices.netsuite.com">*****</urn1:signature>
</urn:tokenPassport>
</soapenv:Header>
<soapenv:Body>
<search xmlns="urn:messages_2020_2.platform.webservices.netsuite.com">
<searchRecord type="FileSearchBasic" xmlns="urn:common_2020_2.platform.webservices.netsuite.com">
<folder type="SearchMultiSelectField" internalId="2760"/>
</searchRecord>
</search>
</soapenv:Body>
However its giving error following error
<faultcode>soapenv:Server.userException</faultcode>
<faultstring>org.xml.sax.SAXException: {urn:core_2020_2.platform.webservices.netsuite.com}SearchRecord is an abstract type and cannot be instantiated</faultstring>
after a lot of research I was able to make a successful soap request to get list of files inside a folder, following is the request body
<soap:Body>
<platformMsgs:search>
<platformMsgs:searchRecord xmlns:s0="urn:filecabinet_2020_2.documents.webservices.netsuite.com" xsi:type="s0:FolderSearchAdvanced">
<s0:criteria>
<s0:basic>
<internalId operator="anyOf">
<searchValue internalId="2760" type="folder" />
</internalId>
</s0:basic>
</s0:criteria>
<s0:columns>
<s0:basic>
<internalId/>
<name/>
</s0:basic>
<fileJoin>
<internalId/>
<name/>
<modified/>
<documentSize/>
</fileJoin>
</s0:columns>
</platformMsgs:searchRecord>
</platformMsgs:search>
</soap:Body>

SharePoint 2010 get all the files and folders recursively

Post: http://******.com/_vti_bin/SiteData.asmx
SOAPAction:
http://schemas.microsoft.com/sharepoint/soap/GetListCollection
request:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetListCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/" />
</soap:Body>
</soap:Envelope>
Using above i get a list of all the top level folders/lists available
After getting all the top level folders using the same information i want to get all the lower level folders and their files using soap requests.
What i have tried is:
Post: http://******.com/_vti_bin/Lists.asmx
Soapaction:
http://schemas.microsoft.com/sharepoint/soap/GetListItems
Request:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<listName>test1</listName>
<QueryOptions>
<IncludeMandatoryColumns>TRUE</IncludeMandatoryColumns>
<ViewAttributes Scope="RecursiveAll"/>
<DateInUtc>TRUE</DateInUtc>
</QueryOptions>
</GetListItems>
</soap:Body>
</soap:Envelope>
which should have returned all the folders and sub folders inside listname test1.
But again it returned only top level files and folders.
What expected is: to return all the files and folders recursively under list test1
Thanks in advance

Perl - SOAP::Lite request not setting xmlns:soap with correct value on Axis2

I've just created a webservice client in Perl using SOAP::Lite.
I manage to invoke a method of my webservice quite easily, but for some unknown reasons, it works sometimes, and sometimes it won't work.
Here's my perl client code :
my $client = SOAP::Lite->uri("http://loa.webservice")
->service("http://localhost:8888/LogAnalyzerWS/services/DataReceiver?wsdl");
my $res;
do {
sleep(2);
print ("ok \n");
$res = $client->sendData($data);
}while(!defined $res);
print $res, "\n";
I tried to add a while loop to resend the data if the result is undefined, but it won't work.
After some analysis of SOAP::Lite trace log file, I found that during the request, one parameter changes.
Here's the correct xml soap request :
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ax21="http://metier.loa/xsd" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:ns="http://webservice.loa" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns:sendData>
<element xsi:type="xs:base64Binary">PD94bWwgdmVyc2lvbj0</element>
</ns:sendData>
</soap:Body>
</soap:Envelope>
And the correct answer :
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:sendDataResponse xmlns:ns="http://webservice.loa">
<ns:return>1</ns:return>
</ns:sendDataResponse>
</soapenv:Body>
</soapenv:Envelope>
And here's the faulty xml request :
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ax21="http://metier.loa/xsd" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:ns="http://webservice.loa" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns:sendData>
<element xsi:type="xs:base64Binary">PD94bWwgdmVyc2lvbj0</element>
</ns:sendData>
</soap:Body>
</soap:Envelope>
With the answer :
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:Action>http://www.w3.org/2005/08/addressing/soap/fault</wsa:Action>
</soapenv:Header>
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:VersionMismatch</faultcode>
<faultstring>Only SOAP 1.1 or SOAP 1.2 messages are supported in the system</faultstring>
<detail />
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
As you can see, xmlns:soap hasn't got the same value in the correct and the faulty request : for the correct one, it is :
"http://schemas.xmlsoap.org/soap/envelope/"
And for the faulty one, it is :
"http://schemas.xmlsoap.org/wsdl/soap/"
Any ideas why SOAP::Lite is changing this parameter on it's own ?
The problem comes from the way namespaces are handled in SOAP::Lite.
See this issue on RT cpan. Axis 2 was generating a wsdl which was then parsed by SOAP::Lite, and the value of xmlns:soap from the hash, set by the SOAP::Lite::Serializer, was wrongly being modified by the parsing of the wsdl.
To solve this problem, use this at the very begining of your code (before setting the wsdl in SOAP::Lite) :
$SOAP::Constants::PREFIX_ENV = 'SOAP-ENV';
Also one can use method envprefix:
$client->envprefix('SOAP-ENV');
This code works for me:
use SOAP::Lite +trace => [ qw(debug) ]; # to get all logging without transport messages
my $service = SOAP::Lite->service('https://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx?WSDL');
$service->soapversion('1.1');
$service->readable(1);
$service->envprefix('SOAP-ENV');
$service->bodyattr({ xmlns => 'http://web.cbr.ru/' });
my $result = $service->GetCursOnDate('2021-02-05T00:00:00');

Datapower Soap Envelope Header Values from Request

I am having a WS Proxy in Datapower . Both the client and the backends are HTTP . My request contains a soap header that has values like :
wsa:Action
wsa:MessageID
wsa:ReplyTo
timestamp
The backend doesnt require all these values , so they are stripped of before sending a cnverted request to the backend and obviously these are not there in the response which I get back from backend. Now when I send a response back to the client from Datapower , I need all these values back in the response soap headers . A
newly created timestamp which expires after 5 mins .
Action
MessageID
ReplyTo
Is there any way to put them back. I dont want to do it from xslt , as I beleive there is some inbuilt support from Datapower to handle this .
I would just store the entire SOAP-header node-set in a context variable and replace it in the response rule. Unless, of course the back-end adds or modifies specific values. In that case, you can pick and choose what to restore.
Please find below code tp remove header and to move it to context variable .
Further you can use that context variable values to place it back.
XSl1 : To save username and password into context variable`
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions"
xmlns:dpconfig="http://www.datapower.com/param/config"
extension-element-prefixes="dp date dpconfig" exclude-result-prefixes=" dp dpconfig ">
<xsl:template match="/">
<xsl:variable name = "User">
<xsl:value-of select="/*[local-name()='Envelope']/*[local-name()='Header']/*[local-name()='Security']/*[local-name()='UsernameToken']/*[local-name()='Username']/text()"/>
</xsl:variable>
<xsl:variable name = "PWD">
<xsl:value-of select="/*[local-name()='Envelope']/*[local-name()='Header']/*[local-name()='Security']/*[local-name()='UsernameToken']/*[local-name()='Password']/text()"/>
</xsl:variable>
<xsl:message dp:priority="debug">
User name : <xsl:value-of select="$User"/>
<xsl:message dp:priority="debug">
Password : <xsl:value-of select="$PWD"/>
</xsl:message>
<dp:set-variable name="'var://context/Test/User'" value ="string($User)"/>
<dp:set-variable name="'var://context/Test/Pws'" value ="string($PWD)"/>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<xsl:copy-of select="/*[local-name()='Envelope']/*[local-name()='Body']/*" />
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
</xsl:stylesheet>
XSl2 :To add back the Username and Password back into SOAP .
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:dpconfig="http://www.datapower.com/param/config"
extension-element-prefixes="dp dpconfig soapenv" exclude-result-prefixes="dp dpconfig">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match = "*[local-name() = 'Header']">
<soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soap:appid xmlns:soap="http://na.az.com/soaplatform">?</soap:appid>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>
<xsl:value-of select="dp:variable('var://context/Test/User')"/>
</wsse:Username>
<wsse:Password>
<xsl:value-of select="dp:variable('var://context/Test/Pws')"/>
</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
</xsl:template>
</xsl:stylesheet>

How to print content of responseXpath () in REST::Client module

I'm using REST::Client perl module to test my REST server. I want to print REST response as usual xml
Currently I'm using
print $client->responseContent()
which prints xml in one line:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:messageReference xmlns:ns2="urn:com:test:xml:rest:mds:1"><messageId>775775</messageId></ns2:messageReference>
The REST::Client module also returns xpath context for the body content by $client->responseXpath(), but I cannot find a way to use it with toString() function from XML::LibXML, which allows to print it as I want:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:messageReference xmlns:ns2="urn:com:test:xml:rest:mds:1">
<messageId>775775</messageId>
</ns2:messageReference>
$client->responseContent() outputs the response as is, if you would like to beautify it use XML::LibXML
my $dom = XML::LibXML->load_xml(string => $client->responseContent());
print $dom->toString();