$request->as_string() returns the request, headers and all. However, it needs to be called AFTER the UserAgent adds its headers. You can force UserAgent to add its headers by calling the UserAgent's prepare_request. To avoid calling prepare_request a second time, substitute a call to the UserAgent's send_request method for the call to the its request method.

In other words, replace

$response = $ua->request($request);

with

$ua->prepare_request($request); print($request->as_string); # $response = $ua->send_request($request);

Uncomment the commented line if you want to both log and send the request.

Similarly, get is just a shortcut to return a request object and passing it to the UserAgent's request method.

$response = $ua->get($url);

is the same thing as

$request = HTTP::Request::Common::GET($url); $response = $ua->request($request);

so replace it with

$request = HTTP::Request::Common::GET($url); $ua->prepare_request($request); print($request->as_string); # $response = $ua->send_request($request);

In reply to Re: how to validate lwp::useragent request? by ikegami
in thread how to validate lwp::useragent request? by goonfest

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.