[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1386
  • Last Modified:

SOAP::LITE - Incorporating XML file into Soap Message

Hi,
I am trying to send a SOAP message which incorporates an XML file but am getting an error message.

Here is the XML file I want to incorporate:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.pip.crif.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ><SOAP-ENV:Body><mns1:getHospitalsList xmlns:mns1="http://ws.pip.crif.com/"><hospitalName></hospitalName><postCode></postCode><userAuth><asUser>COMP028_ch</asUser><password>psw028a2a</password><username>COMP028_a2a</username></userAuth></mns1:getHospitalsList></SOAP-ENV:Body></SOAP-ENV:Envelope>

Here is my script:
#!/usr/bin/perl
###my $WSDL="https://site.net/PosGatewayService.asmx?wsdl=wsdl1";
my $NS = "http://ws.pip.crif.com/";
my $HOST = "http://piptesta2a.crif.com:80/PIP.WS/PIPWS";
use strict;
use SOAP::Lite +trace => qw (debug);
my $soap = SOAP::Lite
      #->readable(1)
      ->xmlschema('http://www.w3.org/2001/XMLSchema')
      ->on_action( sub { join '/', 'http://piptesta2a.crif.com:80/PIP.WS/PIPWS', $_[1]} )
      ->proxy($HOST)
      ->uri($NS);
my $method;
open(my $in, "<getHospitalsList_Request2.txt") or die "Could not open xml: $!\n";
{
local $/;
$method=<$in>;
}
close($in);
my $results = $soap->call($method);

The error message is below.
I don't really understand the error message as there's nothing wrong with my XML, however there is an odd character just before the first element. Can anyone help?


untitled.bmp
0
PERLfisher
Asked:
PERLfisher
  • 17
  • 14
1 Solution
 
kawasCommented:
I think that you are doing it wrong ... If you want to send the XML as a string in a SOAP message, you should do it the following way ($service is the SOAP service name / function name):
my $result = $soap ->$service( SOAP::Data->type( 'string' => "$method" ) )->result

Open in new window

0
 
PERLfisherAuthor Commented:
How do I declare $service?
0
 
PERLfisherAuthor Commented:
Ok, I have amended my script so it is now as follows:

#!/usr/bin/perl

my $NS = "http://ws.pip.crif.com/";
my $HOST = "http://piptesta2a.crif.com:80/PIP.WS/PIPWS";
my $service = ( sub { join '/', 'http://piptesta2a.crif.com:80/PIP.WS/PIPWS', $_[1]} );

#use strict;
use SOAP::Lite ( +trace => all, maptype => {} );
 
my $soap = SOAP::Lite
      #->readable(1)
      ->xmlschema('http://www.w3.org/2001/XMLSchema')
      #->on_action( sub { return 'http://piptesta2a.crif.com:80/PIP.WS/PIPWS'})
      ->on_action( sub { join '/', 'http://piptesta2a.crif.com:80/PIP.WS/PIPWS', $_[1]} )
      ->proxy($HOST)
      ->uri($NS);
#      ->service($service);

my $method;
open(my $in, "<getHospitalsList_Request2.xml") or die "Could not open xml: $!\n";

{local $/;
$method=<$in>; }

close($in);

my $result = $soap ->$service( SOAP::Data->type( 'string' => "$method" ) )->result;

#my $resultsfile = "results.txt";

#open FILE, ">$resultsfile" or die "unable to open $resultsfile $!";

#print FILE $method;

I am now getting the error message attached
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
PERLfisherAuthor Commented:
Error message attached:
Message-2.bmp
0
 
PERLfisherAuthor Commented:
I think I am now very close!

This is my code:

#!/usr/bin/perl

my $NS = "http://ws.pip.crif.com/";
my $HOST = "http://piptesta2a.crif.com:80/PIP.WS/PIPWS";
my $service = "http://piptesta2a.crif.com/PIP.WS/PIPWS?wsdl";
use strict;
use SOAP::Lite +trace => qw (debug);
my $soap = SOAP::Lite
      ->xmlschema('http://www.w3.org/2001/XMLSchema')
      ->on_action( sub { join '/', 'http://piptesta2a.crif.com:80/PIP.WS/PIPWS', $_[1]} )
      ->proxy($HOST)
      ->service($service)
      ->uri($NS);
my $method;
open(my $in, "<getHospitalsList_Request2.xml") or die "Could not open xml: $!\n";
{
local $/;
$method=<$in>;
}
close($in);
my $result = $soap ->$service( SOAP::Data->type( 'string' => "$method" ) )->result;

I seem to be "hitting" the server ok but it is not happy with the name of my method as it is displaying this message:
"Unrecognised method 'http://piptesta2a.crif.com/PIP.WS/PIPWS?wsdl', followed by a list of the available methods. The one I want to call is 'getHospitalsList' and it appears in the list, but whenever I try to reference it specifically
e.g. my $service = "http://piptesta2a.crif.com/PIP.WS/PIPWS?wsdl/getHospitalsList";
I get the same error message.
Help!!!
0
 
kawasCommented:
$service should be getHospitalsList and not the whole URI
0
 
PERLfisherAuthor Commented:
Yes, but I have tried getHospitalsList on its own and I get a "URL must be absolute" message.
0
 
PERLfisherAuthor Commented:
The service (getHospitalsList) is a complextype, does that make any difference?
0
 
kawasCommented:

my $NS = "http://ws.pip.crif.com/";
my $HOST = "http://piptesta2a.crif.com:80/PIP.WS/PIPWS";
my $service = 'getHospitalsList';
my $soap = SOAP::Lite->uri($NS)->proxy($HOST)->on_fault(
		sub {
			my $soap = shift;
			my $res  = shift;
			my $msg =
			  ref $res
			  ? "--- SOAP FAULT ---\n"
			  . $res->faultcode . " "
			  . $res->faultstring
			  : "--- TRANSPORT ERROR ---\n"
			  . $soap->transport->status
			  . "\n$res\n";
			die $msg;
		}
	);
	my $method;
open(my $in, "<getHospitalsList_Request2.xml") or die "Could not open xml: $!\n";
{
local $/;
$method=<$in>;
}
close($in);
	print $soap ->$service( SOAP::Data->type( 'string' => "$method" ) )->result;

Open in new window

0
 
PERLfisherAuthor Commented:
I copied and pasted your code:
Message-3.bmp
0
 
kawasCommented:
your input is incorrect ... can you paste the file that you are sending the webservice?
0
 
PERLfisherAuthor Commented:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.pip.crif.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ><SOAP-ENV:Body><mns1:getHospitalsList xmlns:mns1="http://ws.pip.crif.com/"><hospitalName></hospitalName><postCode></postCode><userAuth><asUser>COMP028_ch</asUser><password>psw028a2a</password><username>COMP028_a2a</username></userAuth></mns1:getHospitalsList></SOAP-ENV:Body></SOAP-ENV:Envelope>
0
 
kawasCommented:
Okay, that is the problem .. you are wrapping the soap message in a soap message ...

your file should only contain something like:

<mns1:getHospitalsList xmlns:mns1="http://ws.pip.crif.com/">
    <hospitalName></hospitalName>
    <postCode></postCode>
    <userAuth>
       <asUser>COMP028_ch</asUser>
       <password>password_here</password>
       <username>username_here</username>
    </userAuth>
</mns1:getHospitalsList>
0
 
PERLfisherAuthor Commented:
I have removed the envelope XML:
Message-4.bmp
0
 
kawasCommented:
Sorry, I had tried to view the wsdl earlier, but failed ... Having just viewed it, your service is a document/literal web service. SOAP lite has limited support for doc lit services.

I would recommend that you use XML::Compile::SOAP (http://search.cpan.org/dist/XML-Compile-SOAP/)

They have pretty decent documentation (http://search.cpan.org/~markov/XML-Compile-SOAP-2.12/lib/XML/Compile/SOAP.pod#SYNOPSIS).

0
 
PERLfisherAuthor Commented:
Oh, I see...
I  can't find XML::Compile::SOAP using ppm. Do you know why? I haven't tried to install a module without ppm before.
0
 
kawasCommented:
Do you have to use windows? I noticed that ppm has an old version (not sure if it makes a huge difference though) of XML-Compile-SOAP (replace :: with -).
0
 
PERLfisherAuthor Commented:
I can't find it using my ppm.
I can download it via Windows, I just wondered where I should save it (Perl is on my C: drive)?
0
 
kawasCommented:

Goto http://trouchelle.com/perl/ppmrepview.pl?  and add the repo to ppm. Once you are done, install the module below:


SOAP-XML-Client (this is the module once you add the repo)
0
 
kawasCommented:
Once the module is installed, the following code should help you get started ... you need to modify some of the values and make sure that http://piptesta2a.crif.com/PIP.WS/PIPWS?wsdl is saved to a text file called pipws.wsdl in the directory that contains your script.
#!/usr/bin/perl -w
use strict;

use XML::Compile::WSDL11;      # use WSDL version 1.1
use XML::Compile::SOAP11;      # use SOAP version 1.1
use XML::Compile::Transport::SOAPHTTP;
use Data::Dumper;


my $wsdl = XML::Compile::WSDL11->new('pipws.wsdl');

# during initiation, for each used call (slow)
my $call = $wsdl->compileClient('getHospitalsList');

# at "run-time", call as often as you want (fast)

my $answer = $call->(hospitalName => '', postCode=> '', userAuth => {asUser => '', password => '', username => ''});
print Dumper($answer);

Open in new window

0
 
PERLfisherAuthor Commented:
I have downloaded SOAP-XML-CLIENT now. I will  have a look at it and see if I can get to grips.
BTW, what is the significance of a document/literal web service?
0
 
kawasCommented:
there are different flavours of SOAP, with each having their own ways of doing things. http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/ is a good reference.
0
 
PERLfisherAuthor Commented:
Success!
This worked:

#!/usr/bin/perl -w
use strict;
use XML::Compile::WSDL11;      # use WSDL version 1.1
use XML::Compile::SOAP11;      # use SOAP version 1.1
use XML::Compile::Transport::SOAPHTTP;
use Data::Dumper;
 my $wsdl = XML::Compile::WSDL11->new('pipws(Stage1).wsdl');
# during initiation, for each used call (slow)
my $call = $wsdl->compileClient('getHospitalsList');
 # at "run-time", call as often as you want (fast)
my $answer = $call->(hospitalName => 'Aberdeen', postCode => '', userAuth => {asUser => 'COMP028_ch', password => 'psw028a2a', username => 'COMP028_a2a'});
print Dumper($answer);

There are 2 things I'd like to change, though:
1. Currently the parameter values (hospitalName, userauth) are hard-coded, whereas I'd like to read them in from the XML file I posted earlier
2, I'd like to output the response to an XML file.
Is this straightforward?
0
 
kawasCommented:
glad to hear that we solved the problem!

with regards to your changes,

1. you could read them in. many options available ... you could read in the XML file and use an XML parser to extract the values that you need. then create your $answer
2. writing a file is as easy as
   open(OUTFILE, '>someFile.xml') or die $!;
   print OUTFILE $answer->NOT_SURE_HERE;
   close OUTFILE;

the NOT_SURE_HERE is the key of the hash of your dump ... if you print out the contents when you run the program, i will tell you exactly what you need.
0
 
PERLfisherAuthor Commented:
Hi.
I have run the following code but am unable to print to my xml file; it looks as though I can't get values out of the hash as easily as I thought! The message output is attached.

#!/usr/bin/perl -w
use strict;
use XML::Compile::WSDL11;      # use WSDL version 1.1
use XML::Compile::SOAP11;      # use SOAP version 1.1
use XML::Compile::Transport::SOAPHTTP;
use Data::Dumper;
my $wsdl = XML::Compile::WSDL11->new('pipws(Stage1).wsdl');
# during initiation, for each used call (slow)
my $call = $wsdl->compileClient('getHospitalsList');
 # at "run-time", call as often as you want (fast)
my $answer = $call->(hospitalName => 'Aberdeen', postCode => '', userAuth => {asUser => 'COMP028_ch', password => 'psw028a2a', username => 'COMP028_a2a'});
print Dumper($answer);
open(OUTFILE, '>someFile.xml') or die $!;
print OUTFILE $answer->{'name'}[1];
close OUTFILE;
Message-6.bmp
0
 
kawasCommented:
this one has the XML ... but my next comment will be better
#!/usr/bin/perl -w
use strict;
use XML::Compile::WSDL11;      # use WSDL version 1.1
use XML::Compile::SOAP11;      # use SOAP version 1.1
use XML::Compile::Transport::SOAPHTTP;
use Data::Dumper;
my $wsdl = XML::Compile::WSDL11->new('pipws(Stage1).wsdl');
# during initiation, for each used call (slow)
my $call = $wsdl->compileClient('getHospitalsList');
 # at "run-time", call as often as you want (fast)
my ($answer, $trace) = $call->(hospitalName => 'Aberdeen', postCode => '', userAuth => {asUser => 'COMP028_ch', password => 'psw028a2a', username => 'COMP028_a2a'});
print Dumper($answer);
open(OUTFILE, '>someFile.xml') or die $!;
print OUTFILE $trace->response->content;
close OUTFILE;

Open in new window

0
 
kawasCommented:

#!/usr/bin/perl -w
use strict;
use XML::Compile::WSDL11;      # use WSDL version 1.1
use XML::Compile::SOAP11;      # use SOAP version 1.1
use XML::Compile::Transport::SOAPHTTP;
use Data::Dumper;
 my $wsdl = XML::Compile::WSDL11->new('pipws.wsdl');
# during initiation, for each used call (slow)
my $call = $wsdl->compileClient('getHospitalsList');
 # at "run-time", call as often as you want (fast)
my ($answer, $trace) = $call->(hospitalName => 'Aberdeen', postCode => '', userAuth => {asUser => 'COMP028_ch', password => 'psw028a2a', username => 'COMP028_a2a'});
print Dumper($answer);

my $output = '';
my $hospitals = $answer->{'getHospitalsListResponse'}->{'hospitalsListResponse'}->{'hospitalsList'};
foreach my $h (@$hospitals) {
  $output .= '<hospitalsList>';
  for my $key (keys %$h) {
     $output = $output . "\n   <$key>" . $h->{$key} . "</$key>";
  }
  $output .= "\n" . '</hospitalsList>' . "\n";
}
open(OUTFILE, '>someFile.xml') or die $!;
print OUTFILE $output;
close OUTFILE;

Open in new window

0
 
PERLfisherAuthor Commented:
This works brilliantly! I am so grateful for your help.
The only thing left is for me to be able to load the request parameters in from an XML file.
0
 
kawasCommented:
This should help you load the parameters from an XML file
my $xml = <<EOF;
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:tns="http://ws.pip.crif.com/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
	 <SOAP-ENV:Body>
		 <mns1:getHospitalsList xmlns:mns1="http://ws.pip.crif.com/">
			 <hospitalName></hospitalName>
			 <postCode></postCode>
			 <userAuth>
				 <asUser>COMP028_ch</asUser>
				 <password>psw028a2a</password>
				 <username>COMP028_a2a</username>
			 </userAuth>
		 </mns1:getHospitalsList>
	 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
EOF

use XML::LibXML;
use XML::LibXML::XPathContext; # not sure if this use is required ...

my $parser = XML::LibXML->new();
my $doc    = $parser->parse_string($xml);

my ($username, $password, $asUser, $hospitalName, $postCode);

my $xpath = "//*[local-name() = 'username']/text()";
my $xpc   = XML::LibXML::XPathContext->new();
$username = $xpc->findvalue( $xpath, $doc->documentElement );

$xpath = "//*[local-name() = 'password']/text()";
$xpc   = XML::LibXML::XPathContext->new();
$password = $xpc->findvalue( $xpath, $doc->documentElement );

$xpath = "//*[local-name() = 'asUser']/text()";
$xpc   = XML::LibXML::XPathContext->new();
$asUser = $xpc->findvalue( $xpath, $doc->documentElement );

$xpath = "//*[local-name() = 'postCode']/text()";
$xpc   = XML::LibXML::XPathContext->new();
$postCode = $xpc->findvalue( $xpath, $doc->documentElement );

$xpath = "//*[local-name() = 'hospitalName']/text()";
$xpc   = XML::LibXML::XPathContext->new();
$hospitalName = $xpc->findvalue( $xpath, $doc->documentElement );

print "$username, $password, $asUser, $postCode $hospitalName\n";

Open in new window

0
 
PERLfisherAuthor Commented:
I will test this, many thanks.
0
 
PERLfisherAuthor Commented:
Fantastic help from Kawas
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 17
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now