lmocsi has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Is it possible to reuse a http::request object with a new url?
Or do I have to construct a new one?
use strict; use LWP::UserAgent; use HTTP::Request; use utf8; my ($content,$decoded,$req,$res); my $url = "http://something"; my $ua = LWP::UserAgent->new; $req = HTTP::Request->new("GET", $url); $req->header('Content-Type' => 'application/json', 'Accept' => 'application/json, text/javascript, */*; q= +0.01' ); $res = $ua->request($req); $content = $res->content(); # First request done # New url $url = "http://something_else"; # What next?

Replies are listed 'Best First'.
Re: How to reuse a http::request object?
by Your Mother (Archbishop) on Dec 05, 2016 at 01:54 UTC

    Don’t do that. It’s not sensible, ulimately, and there are ways to do what you want.

    You probably want default_header(s) in LWP::UserAgent (and WWW::Mechanize is a nicer subclass of it). If you want better HTTP::Request shortcuts, try HTTP::Request::Common

    use strictures; use LWP::UserAgent; # my ($content,$decoded,$req,$res); # ^^^ No! Nothing in the full scope, please. my $ua = LWP::UserAgent->new; $ua->default_header('Content-Type' => 'application/json'); $ua->default_header('Accept' => 'application/json, text/javascript, */ +*; q=0.01'); while ( my $url = your_url_generator_lister() ) { my $response = $ua->get($url); # do whatcha gonna do with it. }
Re: How to reuse a http::request object?
by ikegami (Patriarch) on Dec 05, 2016 at 01:31 UTC

    Note that $res contains references to $req. Changing $req will invalidate some aspects of $res

Re: How to reuse a http::request object?
by BrowserUk (Patriarch) on Dec 04, 2016 at 22:36 UTC

    See the uri() method:

    $r->uri( $val )

    This is used to get/set the uri attribute. The $val can be a reference to a URI object or a plain string. If a string is given, then it should be parseable as an absolute URI.

    Though I'm not at all sure why you wouldn't just construct a new one?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.