AnyEvent::HTTP::Request - HTTP Request object for AnyEvent::HTTP
version 0.302
# parses the same argument list as AnyEvent::HTTP::http_request my $req = AnyEvent::HTTP::Request->new( POST => $uri, body => $body, headers => \%headers, %params, sub { ... } ); # provides introspection print $req->header('user-agent'); print $req->uri; # can be upgraded to an HTTP::Request object my $http_req = $req->to_http_message; # or submitted via AnyEvent::HTTP::http_request $req->send();
This class creates a lightweight object to represent an HTTP request as used by AnyEvent::HTTP.
It was created to provide simple, clear test code for parsing the parameters passed to "http_request" in AnyEvent::HTTP.
Instead of code that looks something like this:
is $args[0], 'POST', 'request method'; is $args[1], 'http://some/where', 'request uri'; is ref($args[-1]), 'CODE', 'http_request callback'; is_deeply { @args[ 2 .. $#args - 1 ] }->{headers}, \%expected_headers, 'request headers';
You can write clearer, simpler code like this:
my $req = AnyEvent::HTTP::Request->new(@args); is $req->method, 'POST', 'request method'; is $req->uri, 'http://some/where', 'request uri'; is ref($req->cb), 'CODE', 'http_request callback'; is_deeply $req->headers, \%expected_headers, 'request headers';
It's a little less weird, and easier to maintain (and do again).
This class also allows you to build an object by passing a hashref of named parameters in case you'd prefer that. You can then call "send" to actually make the request (via "http_request" in AnyEvent::HTTP), or "args" to get the list of arguments the object would pass.
It can also be converted from or to the more featureful HTTP::Request.
Accepts the same argument list as "http_request" in AnyEvent::HTTP (see "parse_args"):
AnyEvent::HTTP::Request->new( $method => $uri, body => $body, headers => \%headers, %params, sub { ... } );
Alternatively accepts an instance of HTTP::Request with an optional hashref of extra attributes (see "from_http_message"):
AnyEvent::HTTP::Request->new( HTTP::Request->new( $method, $uri, $headers, $body ), { cb => sub { ... }, params => \%params, } );
Also accepts a single hashref of named attributes (see "ATTRIBUTES"):
AnyEvent::HTTP::Request->new({ method => 'POST', uri => 'http://example.com', cb => sub { ... }, params => \%params, headers => \%headers, body => $body, });
Called by the constructor to parse the argument list for "http_request" in AnyEvent::HTTP and return a hashref which will be the basis for the object.
The list should look like ($method, $uri, %optional, \&callback) where the %optional hash may include body, headers, and any of the other options accepted by "http_request" in AnyEvent::HTTP (which will become "params").
($method, $uri, %optional, \&callback)
%optional
body
headers
Called by the constructor when "new" is passed an instance of HTTP::Request.
Since only method, uri, headers, and body can be determined from HTTP::Request, a hashref can be passed as a second parameter containing cb and params.
method
uri
cb
params
Request method (GET, POST, etc) (first argument to "http_request" in AnyEvent::HTTP)
Request uri (string) (second argument to "http_request" in AnyEvent::HTTP)
Request content body
Alias for "body"
A hashref of the HTTP request headers
A hashref of the function parameters (optional middle (key => value) arguments to "http_request" in AnyEvent::HTTP)
Note that these are connection params like persistent and timeout, not query params like in CGI.
persistent
timeout
CGI
Note that body and headers will not be included. This hashref is essentially user-agent parameters.
Callback subroutine reference (last argument to "http_request" in AnyEvent::HTTP)
Note: For consistency with the other attributes (and to avoid confusion with other modules) this is a read-only accessor and will croak if passed any arguments.
croak
If you intend to execute the callback (to simulate a response) you can dereference the return value:
$req->cb->($body, $headers);
or use "respond_with".
Returns a list of arguments that can be passed to "http_request" in AnyEvent::HTTP (beware the sub's prototype, though).
$req->respond_with($body, \%headers); $req->respond_with(AnyEvent::HTTP::Response->new(@args)); $req->respond_with(HTTP::Response->new($code, $message, \@headers, $body));
Simulate a response by calling "cb". This method is mostly useful for testing, but then again so is the whole module.
For convenience this method can accept an instance of AnyEvent::HTTP::Response or any list of arguments that can be passed to "new" in AnyEvent::HTTP::Response.
Actually submit the request by passing "args" to "http_request" in AnyEvent::HTTP
Returns an instance of HTTP::Request to provide additional functionality.
Note that "cb" and "params" will not be represented in the HTTP::Request object (since they are for the user-agent and not the request).
AnyEvent::HTTP
AnyEvent::HTTP::Message (base class)
HTTP::Request - More featureful object
Randy Stauner <rwstauner@cpan.org>
This software is copyright (c) 2012 by Randy Stauner.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install AnyEvent::HTTP::Message, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AnyEvent::HTTP::Message
CPAN shell
perl -MCPAN -e shell install AnyEvent::HTTP::Message
For more information on module installation, please visit the detailed CPAN module installation guide.