[Xml-compile] Transport::SOAPHTTP header option

Mark Overmeer mark at overmeer.net
Mon Nov 28 21:08:04 GMT 2011


* Michael Ludwig (m.ludwig at epages.com) [111128 09:35]:
> > So, my main question now is: How can I set/get cookies before/after a
> > SOAP call?
> 
> Take control of the user agent to configure it and have it eat
> as many cookies as you want.

It is a recurring question. I have now taken your example, Michael, and
extended it a bit for the XML::Compile::SOAP::FAQ  This is the text I
have now... Comments?

  MarkOv

=head3 Adding HTTP headers

Some applications require to add headers to the HTTP headers sent or
check headers which are received. SOAP is not about HTTP, so you have
to dive deeper in the underlaying constructs; you have to construct the
code references in more steps, not using the auto-generation mechanisms
of some objects, by default hidden to you.

The ::WSDL11 module detects that the soap-http protocol is needed.
(There is also a pure http protocol defined in the SOAP spec, which
is never used)  When the operation gets compiled (with compileClient),
the ::SOAPHTTP module is used to create the soap-http specific message
transport logic. On its turn, that module uses LWP to do the actual
HTTP exchange. To be able to access the in- and outgoing messages,
you have to reach to that LWP::UserAgent.

Michael Ludwig contributed the following example (slightly adapted)

  my $ua = LWP::UserAgent->new;

  # First the HTTP logic
  # defaults when SSL is used
  $ua->ssl_opts(verify_hostname => 0, keep_alive => 1);

  # Auto-use cookies
  $ua->cookie_jar( {file => $my_jar_file
    , autosave => 1, ignore_discard => 1 });

  # Now, we need the SOAP logic
  my $trans = XML::Compile::Transport::SOAPHTTP
    ->new(user_agent => $ua, timeout => 10, address => $srv_url);

  # Finally the message, with explicit transporter
  my $call = $wsdl->compileClient($opname, transport => $trans);

  # $answer is the decoded XML content.
  my($answer, $trace) = $call->( \%parms );

  # If you need headers from the response HTTP headers.
  my $http_response = $trace->response;
  print $http_response->header('X-Secret');

You may know the C<$srv_url> to get the address of the server, but
you can also ask the operation itself. Here a different implementation:

  my $op    = $wsdl->operation($opname);
  my $srv   = ($op->addresses)[0];
  my $trans = XML::Compile::Transport::SOAPHTTP->new(address => $srv);

  # Now configure the userAgent
  my $ua    = $trans->userAgent;
  ...

  my $call  = $op->compileClient(transport => $trans);
  ...

The L<LWP::UserAgent> has many useful hooks (<i>Handlers</i>), for
instance C<request_send> and C<response_done>.



More information about the Xml-compile mailing list