GetSpeech Worked - Using SOAP::Data Class and on_action() Function

This section provides a tutorial example on how to use the SOAP::Data class to build the request parameter element with correct element name and a namespace. My first SOAP::Lite program works now with the GetSpeech Web service.

In the previous tutorial, I confirmed that the parameter name "c-gensym3" generated with SOAP::Lite default setting is the root cause of the exception of "System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object."

What SOAP::Lite function should I use to fix this issue? Looking at the ActivePerl SOAP::Lite manual again, I found this class:

SOAP::Data
   You can use this class if you want to specify a value, a name, 
   atype, a uri or attributes for SOAP elements (use value(), name(), 
   type(), uri() and attr() methods correspondingly). For example, 
   SOAP::Data->name('abc')->value(123) will be serialized into 
   123, as well as will SOAP::Data->name(abc => 123). Each 
   of them (except the value() method) can accept a value as the 
   second parameter. All methods return the current value if you call 
   them without parameters. The return the object otherwise, so you 
   can stack them.

Based on the above description I revised my test program as GetSpeech_Parameter_Name.pl:

#- GetSpeech_Parameter_Name.pl
#- Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
#- All rights reserved
#
   use SOAP::Lite +trace;
   my $client = SOAP::Lite->new();
   $client->proxy('http://www.xmlme.com/WSShakespeare.asmx');

#- Fixing the SOAPAction header line - join uri and method name with /
   $client->uri('http://xmlme.com/WebServices');
   $client->on_action(sub { join '/', @_ });

#- Building the parameter element with name and namespace
   my $parameter = SOAP::Data
        ->uri('http://xmlme.com/WebServices')
        ->name(Request)
        ->value("To be, or not to be");

#- Calling the server
   my $som = $client->GetSpeech($parameter);
   my $output = $som->result;
   print $output . "\n";

Here is what I did in this program:

When executing GetSpeech_Parameter_Name.pl, I got this:

\herong>GetSpeech_Parameter_Name.pl
...
POST http://www.xmlme.com/WSShakespeare.asmx
Accept: text/xml
Accept: multipart/*
Content-Length: 597
Content-Type: text/xml; charset=utf-8
SOAPAction: http://xmlme.com/WebServices/GetSpeech

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
  xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/1999/XMLSchema"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <namesp1:GetSpeech xmlns:namesp1="http://xmlme.com/WebServices">
      <namesp2:Request xsi:type="xsd:string"
        xmlns:namesp2="http://xmlme.com/WebServices">
        To be, or not to be
      </namesp2:Request>
    </namesp1:GetSpeech>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
...
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Connection: close
Date: ... 2009
Server: Microsoft-IIS/6.0
Content-Length: 1942
Content-Type: text/xml; charset=utf-8
Client-Date: ... 2009
Client-Peer: ...
Client-Response-Num: 1
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET

<?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>
    <GetSpeechResponse xmlns="http://xmlme.com/WebServices">
      <GetSpeechResult><SPEECH><PLAY>HAMLET</PLAY>
        <SPEAKER>HAMLET</SPEAKER>To be, or not to be: 
        that is the question: Whether 'tis nobler in the mind
        ...</SPEECH>
      </GetSpeechResult>
    </GetSpeechResponse>
  </soap:Body>
</soap:Envelope>
...

Bingo! I got my first SOAP::Lite program working with GetSpeech Web service provided by xmlme.com.

Last update: 2009.

Table of Contents

 About This Book

 Introduction to Web Service

 Introduction to SOAP (Simple Object Access Protocol)

 SOAP Message Structure

 SOAP Message Transmission and Processing

 SOAP Data Model

 SOAP Encoding

 SOAP RPC Presentation

 SOAP Properties Model

 SOAP Message Exchange Patterns

 SOAP HTTP Binding

 SOAP Perl Implementations

 SOAP PHP Implementations

 SOAP Java Implementations

 Perl SOAP::Lite - SOAP Server-Client Communication Module

 Perl Socket Test Program for HTTP and SOAP

Perl SOAP::Lite for GetSpeech SOAP 1.1 Web Service

 GetSpeech Failed - Incorrect SOAPAction Header Line

 GetSpeech Failed - Incorrect Parameter Name c-gensym

 Using Socket Program to Request Parameter Name

GetSpeech Worked - Using SOAP::Data Class and on_action() Function

 GetSpeech_SOAP_1_1.pl - SOAP::Lite for SOAP 1.1 Web Service

 Perl SOAP::Lite 0.710 for SOAP 1.2 Web Services

 Perl SOAP::Lite 0.710 for WSDL

 PHP SOAP Extension Client Programs

 PHP SOAP Extension Server Programs

 Java Socket and HttpURLConnection for SOAP

 SAAJ - SOAP with Attachments API for Java

 SoapUI - SOAP Web Service Testing Tool

 WS-Security - SOAP Message Security Extension

 WS-Security X.509 Certificate Token

 Web Services and SOAP Terminology

 References

 PDF Printing Version