A common question in the SOAP::Lite Newsgroup is about intercepting the request prior to handling, and the response prior to it being returned. SOAP::Lite v0.65 enables this on the client side pretty easily using the http_request and http_response methods on SOAP::Transport::HTTP::Client, but the server side is still a relative black art. I have friends who have used SOAP::Trace to log, or write to a file, the SOAP payload, but that is not entirely sufficient for all their needs. In future versions, SOAP::Lite will introduce the concept of handler chains, similar to Java Servlets, but for now, one still needs to do a little more work. The following code sample shows how one can extend SOAP::Transport::HTTP:CGI to intercept both the HTTP request and the HTTP response prior to it being processed and/or returned respectively.
One disadvantage to this approach is the fact that the HTTP::Request and HTTP::Response objects have not yet been marshalled when handle and make_response are called. In future versions obviously, this will be fixed, but for the time being - these are the limitations. Of course one is always encouraged to completely override SOAP::Transport::HTTP:CGI and marshall the LWP objects themselves! :-)
#!/usr/bin/perl -w
# filename: soaplite-test.cgi
use SOAP::Transport::HTTP;
use MIME::Entity;
My::CGI::Handler ->dispatch_to('TemperatureService') ->handle;
BEGIN {
#####################################################################
package My::CGI::Handler;
use vars qw(@ISA);
@ISA = qw(SOAP::Transport::HTTP::CGI);
sub handle {
my $self = shift;
print STDERR "My::CGI::Handler => inside\n";
print STDERR "My::CGI::Handler => content-type: ". $ENV{'SCRIPT_NAME'}."\n";
return $self->SUPER::handle;
}
sub make_response {
my $self = shift;
my($code, $response) = @_;
print STDERR "About to call My::CGI::Handler::make_response\n";
return $self->SUPER::make_response($code, $response);
}
###############################################################
package TemperatureService;
use vars qw(@ISA);
@ISA = qw(Exporter SOAP::Server::Parameters);
use SOAP::Lite;
sub c2f {
my $self = shift;
my $envelope = pop;
my $temp = $envelope->dataof("//c2f/temperature");
return SOAP::Data->name('convertedTemp' => (((9/5)*($temp->value)) + 32));
}
1;
# Don't forget this line:!!! ###############################################################
}
In the SOAP::Trace documentation, the 'debug' signal is documented as "Client or Server", but it is a client only signal.
If you want to trace request and response as string also when acting as a soap server, you must add 2 lines in /SOAP/Transport/HTTP.pm file:
sub handle {
my $self = shift->new;
SOAP::Trace::debug($self->request->as_string); # log incoming request
...
}
sub make_response {
...
SOAP::Trace::debug($self->response->as_string); # log outgoing response
}
What do you think about adding them in the next release?
Thanks.
Luigino.
Posted by: Luigino Masarati | April 26, 2007 at 04:27 PM
Hi,
We are using http->request(url) to retrieve PDF file from the Server. This works fine if the server is up and running.
In case, if the server is down (i.e. if the main server is down then there is a leveler( intermediate ) circuit which redirects into a back-up server ) then this function fails and the error code is 302. Please let us know, if there is any alternate method.......
Existing Code:-
$http = new HTTP::Lite;
$req = $http->request ($reqStr)
I need your help as Perl scripting and Web applications concepts are completely new to me.
Also, please let me know, if there are any other modules (like HTTP::Lite) which I can use.
-Regards
Praveen
Posted by: praveen castelino | June 01, 2007 at 07:45 AM
but for now, one still needs to do a little more work. The following code sample shows how one can extend SOAP::Transport::HTTP:CGI to intercept both the HTTP request and the HTTP response prior to it being processed and/or returned respectively.
Posted by: star trek gold | July 25, 2010 at 10:48 PM