in reply to Re^2: LWP is there any way to get "real" outgoing headers?
in thread LWP is there any way to get "real" outgoing headers?
But of course I can't use global variables in my real code, since it will end with complete mess. So I have another problem - I have no idea how to put "$outgoing_headers" in private variable (I'll post my pseudo code with sub below):
You can't get away from global variables, you're monkeypatching :) but ok
#!/usr/bin/perl -- use strict; use warnings; use LWP; Main( @ARGV ); exit( 0 ); sub snoop { use Net::HTTP::Methods; ## important my( $url ) = @_; no warnings 'redefine'; my $original = \&Net::HTTP::Methods::format_request; my $outgoing_headers = ''; local *Net::HTTP::Methods::format_request = sub { $outgoing_headers = $original->(@_); };; my $ua = LWP::UserAgent->new; my $response = $ua->get($url); warn "[Headers Out Real]\n", $outgoing_headers, "\n\n"; warn "[Headers Out]\n", $response->request()->as_string(), "\n\n"; warn "[Headers In]:\n", $response->headers()->as_string, "\n\n"; } sub Main { snoop( 'http://example.com' ); #~ snoop( 'https://example.com' );## duh, internal response doesn' +t trigger format_request snoop( 'https://google.com' ); } __END__
To not monkey patch you can always implement your own LWP::Protocol::Ahttp and LWP::Protocol::Ahttps where you save the format_request somewhere in the request/response/or/lwp object , and register them, but that's just a different kind of global :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: LWP is there any way to get "real" outgoing headers?
by Anonymous Monk on May 25, 2014 at 22:40 UTC |