NAME

AnyEvent::HTTP::Request - HTTP Request object for AnyEvent::HTTP

VERSION

version 0.100

SYNOPSIS

# parse argument list for AnyEvent::HTTP::http_request
AnyEvent::HTTP::Request->new(GET => $uri, %params, sub { ... });

# or use a hashref of named arguments
AnyEvent::HTTP::Request->new({
  method  => 'POST',
  uri     => 'http://example.com',
  cb      => sub { ... },
  params  => \%params,
  headers => \%headers,
  body    => $body,
});

DESCRIPTION

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.

CLASS METHODS

new

See "SYNOPSIS" for usage example.

Accepts a list of arguments (like those that would be passed to "http_request" in AnyEvent::HTTP) which will be passed through "parse_args".

Alternatively a single hashref can be passed with anything listed in "ATTRIBUTES" as the keys.

parse_args

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, %params, \&callback).

ATTRIBUTES

method

Request method (GET, POST, etc) (first argument to "http_request" in AnyEvent::HTTP)

uri

Request uri (string) (second argument to "http_request" in AnyEvent::HTTP)

cb

Callback subroutine reference (last argument to "http_request" in AnyEvent::HTTP)

params

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.

headers

A hashref of the HTTP request headers (the headers key of "params")

body

Request content body (if any) (the body key of "params")

content

Alias for "body"

METHODS

args

Returns a list of arguments that can be passed to "http_request" in AnyEvent::HTTP (beware the sub's prototype, though).

send

Actually submit the request by passing "args" to "http_request" in AnyEvent::HTTP

TODO

SEE ALSO

AUTHOR

Randy Stauner <rwstauner@cpan.org>

COPYRIGHT AND LICENSE

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.