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();
Related
As mentioned in frisby official document (http://frisbyjs.com/) , I am using --junitreport something like following
jasmine-node ./demo/validation_spec.js/ --junitreport --output
C:\Users\Administrator\Documents\script/Reports
which is generating 15 xml files. As I am calling 15 time post using for loop. File contains data like following
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites>
<testsuite name="Frisby Test:blank action " errors="0" tests="1" failures="0" time="0.284" timestamp="2017-03-21T13:55:15">
<testcase classname="Frisby Test: blank action " name="
[ POST https://localhost:8443/api/v2/settings/pointwise ]" time="0.282"></testcase>
</testsuite>
</testsuites>
so my difficulty is that I need to read each and every file to check if tests is pass or not. I want some kind of overview page like http://maven.apache.org/surefire/maven-surefire-report-plugin/surefire-report.html
which contain information how many test passed, fail etc. Is it possible to get that.
I've got a very simple xml, as below:
<?xml version="1.0" encoding="utf-8"?>
<First>
<Second>
<Folder>today</Folder>
<FileCount>10</FileCount>
</Second>
<Second>
<Folder>tomorrow</Folder>
<FileCount>90</FileCount>
</Second>
<Second>
<Folder>yesterday</Folder>
<FileCount>22</FileCount>
</Second>
</First>
Then I have a powershell script to select "Folder" element:
[xml]$xml=Get-Content "D:\m.xml"
$xml.SelectNodes("//Folder")
It outputs:
#text
-----
today
tomorrow
yesterday
No problem. But if I change the xml file to add "xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to "First" like below:
<?xml version="1.0" encoding="utf-8"?>
<First xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Second>
<Folder>today</Folder>
<FileCount>10</FileCount>
</Second>
<Second>
<Folder>tomorrow</Folder>
<FileCount>90</FileCount>
</Second>
<Second>
<Folder>yesterday</Folder>
<FileCount>22</FileCount>
</Second>
</First>
Then, my powershell script outputs nothing.
Why? How to change my powershell script to support this xmlns?
Thanks a lot.
What you added is default namespace. Unlike prefixed namespace, descendant elements inherit ancestor default namespace implicitly, unless otherwise specified (using explicit prefix or local default namespace that points to different URI).
To select element in namespace, you'll need to define prefix that point to the namespace URI and use that prefix properly in your XPath, for example :
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("d", $xml.DocumentElement.NamespaceURI)
$xml.SelectNodes("//d:Folder", $ns)
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');
I am sending the following curl command from terminal in Mac OSX:
curl -d "OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=1AD….4&INPUT_DATA=TestData.xml" http://xxx.xx.xx.xx/sdpapi/request/
I am getting back the response:
FailedError when performing - ADD_REQUEST - Content is not allowed in prolog.
Here is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Operation>
<Details>
<requester>Me</requester>
<subject>Test</subject>
<description>Testing curl input</description>
</Details>
</Operation>
I have checked and my xml file is indeed a UTF-8 file. I can tell from google searches that this most likely has to do with my encoding, however, I can't find how to fix it.
I've also tried saving the xml file as ANSI on my pc, xml file looks like this:
<?xml version="1.0"?>
<Operation>
<Details>
<requester>Me</requester>
<subject>Test</subject>
<description>Testing curl input</description>
</Details>
</Operation>
I downloaded Notepad++ and checked the encoding, which is UTF8 with no BOM.
I am still getting the same error. Can anyone see what I am doing wrong?
Updated 9/19 to add:
In addition to everything I've tried in the comments below, I've also tried this:
curl -d "OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=xxxxxxxxxxxxxxxxx&INPUT_DATA=<?xml version="1.0" encoding="utf-8"?><Operation><Details><requester>Me</requester><subject>Test</subject><description>Testing curl input</description></Details></Operation>" http://xxx.xx.xx.xx/sdpapi/request/
The error I'm getting now is: "Error when performing - ADD_REQUEST - The value following "version" in the XML declaration must be a quoted string."
Anyone have any thoughts?
I replaced <?xml version="1.0" encoding="utf-8"?> with <?xml version=%221.0%22 encoding=%22utf-8%22?>
It is now working.
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.