1

I'm writing my first perl script for the requirement

  • generate HTTP request against a particular web uri in succession using different URL scheme patterns
use HTTP::Request::Generator 'generate_requests';
use URI;
use HTTP::Request::Common;
use strict; # safety net
use warnings; # safety ne
use Test::LWP::UserAgent 'send_request';
use LWP::UserAgent 'send_request';
use Test::More;
use URI;
use HTTP::Request::Common;
use LWP::UserAgent;



my $g = generate_requests(
    method => 'POST',
    host   => ['example.com','www.example.com'],
    pattern => 'https://example.com/{bar,foo,gallery}/[00..99].html',


    wrap => sub {
        my( $req ) = @_;
        # Fix up some values
        $req->{headers}->{'Content-Length'} = 666;
    },
);
while( my $r = $g->()) {
    send_request( $r );
};

I'm using atom editor and activeperl on windows 10, I get following error from running above code.

Undefined subroutine &main::send_request called at C:\Users\ADMINI~1\AppData\Local\Temp\atom_script_tempfiles\0ac821e0-0886-11eb-9588-291dbc37d883 line 57.

I have already installed all necessary modules and lib but i think its unable to refer the method send_request. Pls assist.

NOTE I have replaced real values in variable for privacy reasons.

UPDATE

I plan to use following module pattern => 'https://example.{com,org,net}/page_[00..99].html', from https://metacpan.org/pod/HTTP::Request::Generator.

user316389
  • 87
  • 7
  • Looks like you don't actually solve the problem you've been asked to solve. All of your generated requests will use the same URL scheme - https. – Dave Cross Oct 07 '20 at 10:46
  • @DaveCross thats my inital code in real it be https://metacpan.org/pod/HTTP::Request::Generator done through `pattern => 'https://example.{com,org,net}/page_[00..99].html'` – user316389 Oct 07 '20 at 11:01
  • I guess my point is that HTTP::Request::Generator might be overkill for a task where you just want to change HTTP to HTTPS :-) – Dave Cross Oct 07 '20 at 11:02
  • @DaveCross infact i moved to this custom solution because this failed for me https://stackoverflow.com/questions/64195729/stress-testing-uri-using-xargs-curls-bash-script-failing-with-status-empty. I want to avoid timeout condition for 504 http requests..thats my main goal. – user316389 Oct 07 '20 at 11:06
  • I very much doubt that changing your client software will fix 504 problems. You will still be sending the same requests (or something vert similar). – Dave Cross Oct 07 '20 at 12:10
  • @DaveCross so its something that cannot be controlled at client side? – user316389 Oct 07 '20 at 12:41
  • Well, It's not impossible, but it seems unlikely. It means that the server is taking too long to respond. – Dave Cross Oct 07 '20 at 12:58

1 Answers1

4

LWP::UserAgent is an object-oriented module. It doesn't export functions. You want to call send_request like this:

my $ua = 'LWP::UserAgent'->new;

while ( my $r = $g->() ) {
    $ua->send_request( $r );
}

That said, send_request is an undocumented internal method. I think it is probably more intended for people who are subclassing LWP::UserAgent. You probably want the request method instead.

my $ua = 'LWP::UserAgent'->new;

while ( my $r = $g->() ) {
    my $response = $ua->request( $r );
}

Full code:

use strict;
use warnings;
use HTTP::Request::Generator 'generate_requests';
use LWP::UserAgent;

my $ua = 'LWP::UserAgent'->new;

my $gen = generate_requests(
    method  => 'POST',
    host    => [ 'example.com', 'www.example.com' ],
    pattern => 'https://example.com/{bar,foo,gallery}/[00..99].html',
    wrap    => sub {
        my ( $req ) = @_;
        # Fix up some values
        $req->{'headers'}{'Content-Length'} = 666;
    },
);

while ( my $req = $gen->() ) {
    my $response = $ua->request( $req );
    
    # Do something with $response here?
}
tobyink
  • 13,478
  • 1
  • 23
  • 35
  • I ran your code, it says `You need a request object, not '666' at E:\501dos.pl line 20.` Do i need to put quotes around `666` this is just an example infact i want to pass many headers so `headers => [ { "Content-Type" => 'text/plain; encoding=UTF-8', Cookie => 'my_session_id' },` – user316389 Oct 07 '20 at 10:58
  • 1
    @user316389 Hmm, I think perhaps the `wrap` sub needs a `return $req` at the end. The documentation for HTTP::Request::Generator doesn't show it though. – tobyink Oct 07 '20 at 11:12
  • if i do that i get new error `You need a request object, not a HASH object at E:\501dos.pl line 21. ` – user316389 Oct 07 '20 at 11:14
  • @user316389 what's your entire `wrap` sub? Whatever it is, it's perhaps not generating an HTTP::Request object. – tobyink Oct 07 '20 at 11:20
  • ` wrap => \&HTTP::Request::Generator::as_http_request, ` i was missing this line it was way at the end of `https://metacpan.org/pod/HTTP::Request::Generator` let me update original question. – user316389 Oct 07 '20 at 11:25