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 HerongYang.com. All Rights Reserved.
#- 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 HerongYang.com. All Rights Reserved.
#- 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!

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 MEP (Message Exchange Patterns)

 SOAP HTTP Binding

 SOAP PHP Implementations

 PHP SOAP Extension Client Programs

 PHP SOAP Extension Server Programs

 PHP SOAP Web Service Example - getTemp

 SOAP Perl Implementations

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

 Perl Socket Test Program for HTTP and SOAP

 Perl SOAP::Lite for NumberToWords SOAP 1.1 Web Service

 Perl SOAP::Lite for SOAP 1.2 Web Services

 Perl SOAP::Lite for WSDL

 Python SOAP Client: Zeep

 SOAP Java Implementations

 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

 Perl SOAP::Lite for GetSpeech SOAP 1.1 Web Service

Perl SOAP::Lite 0.710 for SOAP 1.2 Web Services

 Installing SOAP::Lite 0.710 to Support SOAP 1.2

 GetSpeech Request Differences between SOAP 1.1 and 1.2

 Unsupported Media Type: "application/soap"

 DEFAULT_HTTP_CONTENT_TYPE='application/soap+xml'

content_type() method in the HTTP::Headers Class

 Perl SOAP::Lite 0.710 for WSDL

 Web Services and SOAP Terminology

 Archived Tutorials

 References

 Full Version in PDF/EPUB