HTTP::Request::Generator - generate HTTP requests
use HTTP::Request::Generator 'generate_requests'; @requests = generate_requests( method => 'GET', pattern => 'https://example.com/{bar,foo,gallery}/[00..99].html', ); # generates 300 requests from # https://example.com/bar/00.html to # https://example.com/gallery/99.html @requests = generate_requests( method => 'POST', url => '/profiles/:name', url_params => { name => ['Corion','Co-Rion'], }, query_params => { stars => [2,3], }, body_params => { comment => ['Some comment', 'Another comment, A++'], }, headers => [ { "Content-Type" => 'text/plain; encoding=UTF-8', Cookie => 'my_session_id', }, { "Content-Type" => 'text/plain; encoding=Latin-1', Cookie => 'my_session_id', }, ], ); # Generates 16 requests out of the combinations for my $req (@requests) { $ua->request( $req ); };
generate_requests( %options )
my $g = generate_requests( url => '/profiles/:name', url_params => ['Mark','John'], wrap => sub { my( $req ) = @_; # Fix up some values $req->{headers}->{'Content-Length'} = 666; }, ); while( my $r = $g->()) { send_request( $r ); };
This function creates data structures that are suitable for sending off a mass of similar but different HTTP requests. All array references are expanded into the cartesian product of their contents. The above example would create two requests:
url => '/profiles/Mark, url => '/profiles/John',
generate_requests returns an iterator in scalar context. In list context, it returns the complete list of requests.
generate_requests
There are helper functions that will turn that data into a data structure suitable for your HTTP framework of choice.
{ method => 'GET', url => '/profiles/Mark', protocol => 'http', port => 80, headers => {}, body_params => {}, query_params => {}, }
As a shorthand for creating lists, you can use the pattern option, which will expand a string into a set of requests. {} will expand into alternatives while [xx..yy] will expand into the range xx to yy. Note that these lists will be expanded in memory.
pattern
{}
[xx..yy]
xx
yy
Generate URLs from this pattern instead of query_params, url_params and url.
query_params
url_params
url
URL template to use.
Parameters to replace in the url template.
Parameters to replace in the POST body.
Parameters to replace in the GET request.
Hostname(s) to use.
Port(s) to use.
Headers to use. Currently, no templates are generated for the headers. You have to specify complete sets of headers for each alternative.
Limit the number of requests generated.
as_http_request
generate_requests( method => 'POST', url => '/feedback/:item', wrap => \&HTTP::Request::Generator::as_http_request, )
Converts the request data to a HTTP::Request object.
as_dancer
generate_requests( method => 'POST', url => '/feedback/:item', wrap => \&HTTP::Request::Generator::as_dancer, )
Converts the request data to a Dancer::Request object.
as_plack
generate_requests( method => 'POST', url => '/feedback/:item', wrap => \&HTTP::Request::Generator::as_plack, )
Converts the request data to a Plack::Request object.
The Curl Manpage for the pattern syntax
The public repository of this module is https://github.com/Corion/HTTP-Request-Generator.
The public support forum of this module is https://perlmonks.org/.
Please report bugs in this module via the RT CPAN bug queue at https://rt.cpan.org/Public/Dist/Display.html?Name=HTTP-Request-Generator or via mail to HTTP-Request-Generator-Bugs@rt.cpan.org.
Max Maischein corion@cpan.org
corion@cpan.org
Copyright 2017-2018 by Max Maischein corion@cpan.org.
This module is released under the same terms as Perl itself.
To install HTTP::Request::Generator, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTTP::Request::Generator
CPAN shell
perl -MCPAN -e shell install HTTP::Request::Generator
For more information on module installation, please visit the detailed CPAN module installation guide.