Herong's Tutorial Notes on Web Service and SOAP
Dr. Herong Yang, Version 4.00

content_type() method in the HTTP::Headers Class

This section provides a tutorial example on how to use the content_type() method in the HTTP::Headers class to set the Content-Type header line with 'application/soap+xml'.

The second option of getting "Content-Type: application/soap+xml" is to set content_type in object $http_request explicitly.

To do this we need to find a way to reach $http_request inside $client. Since Perl objects are all hash tables, the Data::Dumper can help us to see the inner structure of the $client hash table:

#- GetSpeech_Data_Dumper.pl
#- Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
#- All rights reserved
#
   use Data::Dumper;
   use SOAP::Lite;
   my $client = SOAP::Lite->new()
      ->soapversion('1.2')
      ->envprefix('soap12')
      ->default_ns('http://xmlme.com/WebServices')
      ->on_action( sub {join '/', @_} )
      ->readable(true)
      ->proxy('http://localhost/WSShakespeare.asmx');

   print Dumper($client);

Result of GetSpeech_Default_HTTP_Content_Type.pl:

$VAR1 = bless( {
     '_on_nonserialized' => sub { "DUMMY" },
     '_deserializer' => bless( {...
        }, 'SOAP::Deserializer' ),
     '_autoresult' => 0,
     '_transport' => bless( {
          '_proxy' => bless( {
               ...
               '_http_request' => bless( {
                    '_content' => '',
                    '_uri' => undef,
                    '_headers' => bless( {}, 'HTTP::Headers' ),
                    '_method' => undef
                  }, 'HTTP::Request' ),
               ...
             }, 'SOAP::Transport::HTTP::Client' )
        }, 'SOAP::Transport' ),
     '_serializer' => bless( {...
        }, 'SOAP::Serializer' ),
     '_schema' => undef,
     '_packager' => bless( {...
        }, 'SOAP::Packager::MIME' ),
     '_on_action' => sub { "DUMMY" },
     '_on_fault' => sub { "DUMMY" }
   }, 'SOAP::Lite' );

The dumper output tells me that $http_request is an object of HTTP::Request class I can get the $http_request object by follow those pointers as:

   $http_request = $client
      ->{'_transport'}
      ->{'_proxy'}
      ->{'_http_request'};

I read the HTTP::Request manual. It inherits the method content_type() from the HTTP::Headers class. So my solution will be calling content_type() to set Content-Type after my $client object is ready.

#- GetSpeech_Content-Type_Method.pl
#- Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
#- All rights reserved
#
   use SOAP::Lite +trace;
   my $client = SOAP::Lite->new()
      ->soapversion('1.2')
      ->envprefix('soap12')
      ->default_ns('http://xmlme.com/WebServices')
      ->on_action( sub {join '/', @_} )
      ->readable(true)
      ->proxy('http://localhost/WSShakespeare.asmx');

#- Setting Content-Type myself
   my $http_request = $client
      ->{'_transport'}
      ->{'_proxy'}
      ->{'_http_request'};
   $http_request->content_type('application/soap+xml');

   my $som = $client->call('GetSpeech', 
      SOAP::Data->name("Request")
      ->value("To be, or not to be")
   );

Result of GetSpeech_Content-Type_Method.pl:

POST http://localhost/WSShakespeare.asmx HTTP/1.1
Accept: text/xml
Accept: multipart/*
Accept: application/soap
Content-Length: 606
Content-Type: application/soap+xml; charset=utf-8
SOAPAction: http://xmlme.com/WebServices/GetSpeech
...

It worked too!

Last update: 2009.

Sections in This Chapter

Installing SOAP::Lite 0.710 to Support SOAP 1.2

Features in SOAP::Lite 0.710

Methods on SOAP::Lite 0.710 Client Object

Testing SOAP::Lite Client Objects

Request Differences between SOAP 1.1 and SOAP 1.2

GetSpeech_localhost.pl - Testing GetSpeech on Local Host

soapversion('1.2') and envprefix('soap12') Must Used Together

default_ns() - Setting Default namespace for Body Elements

SOAP::Data - Utility Class to Generate XML Elements

SOAPAction - Not Needed, But No Way to Remove It

Unsupported Media Type: "application/soap"

DEFAULT_HTTP_CONTENT_TYPE='application/soap+xml'

content_type() method in the HTTP::Headers Class

GetSpeech_SOAP_1_2.pl - SOAP::Lite for SOAP 1.2 Web Service

Dr. Herong Yang, updated in 2009
content_type() method in the HTTP::Headers Class