I changed you code a bit to be able to use it with both HTTP and SSL/TLS requests, so it's looks like this now:

#!/usr/bin/env perl -w use strict; use warnings; use LWP; my $outgoing_headers = ''; sub LWP::Protocol::http::Socket::format_request { return main::format_ +request(@_); } sub LWP::Protocol::https::Socket::format_request { return main::format +_request(@_); } sub format_request { my( $socket, $method, $fullpath, @h ) = @_; my $req_buf = $socket->Net::HTTP::Methods::format_request($method, + $fullpath, @h); $outgoing_headers = $req_buf; return $req_buf; } my $ua = new LWP::UserAgent; my $response = $ua->get("http://example.com"); #my $response = $ua->get("https://google.com"); 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";

It working great for test purpose. :)
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):

#!/usr/bin/env perl -w use strict; use warnings; use LWP; sub LWP::Protocol::http::Socket::format_request { return main::format_ +request(@_); } sub LWP::Protocol::https::Socket::format_request { return main::format +_request(@_); } sub format_request { my( $socket, $method, $fullpath, @h ) = @_; my $req_buf = $socket->Net::HTTP::Methods::format_request($method, + $fullpath, @h); # $outgoing_headers = $req_buf; return $req_buf; } sub make_request { my ($url) = @_; my $outgoing_headers = ''; # the question is - how to put them +here? my $ua = new LWP::UserAgent; 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"; } make_request('http://example.com');

Any idea how I can do this? :)


In reply to Re^2: LWP is there any way to get "real" outgoing headers? by Anonymous Monk
in thread LWP is there any way to get "real" outgoing headers? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.