The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

AnyEvent::UserAgent - AnyEvent::HTTP OO-wrapper

SYNOPSIS

use AnyEvent::UserAgent;
use Data::Dumper;

my $ua = AnyEvent::UserAgent->new;
my $cv = AE::cv;

$ua->get('http://example.com/', sub {
    my ($res) = @_;
    print(Dumper($res, $ua->cookie_jar));
    $cv->send();
});
$cv->recv();

DESCRIPTION

AnyEvent::UserAgent is a OO-wrapper around AnyEvent::HTTP with cookies support by HTTP::Cookies. Also request callback receives response as HTTP::Response object.

ATTRIBUTES

agent

The product token that is used to identify the user agent on the network. The agent value is sent as the User-Agent header in the requests.

The cookie jar object to use. The only requirement is that the cookie jar object must implement the extract_cookies($req) and add_cookie_header($res) methods. These methods will then be invoked by the user agent as requests are sent and responses are received. Normally this will be a HTTP::Cookies object or some subclass. Default cookie jar is the HTTP::Cookies object.

inactivity_timeout

Maximum time in seconds in which connection can be inactive before getting closed. Default timeout value is 20. Setting the value to 0 will allow connections to be inactive indefinitely.

max_redirects

Maximum number of redirects the user agent will follow before it gives up. The default value is 5.

request_timeout

Maximum time in seconds to establish a connection, send the request and receive a response. The request will be canceled when that time expires. Default timeout value is 0. Setting the value to 0 will allow the user agent to wait indefinitely. The timeout will reset for every followed redirect.

METHODS

new

my $ua = AnyEvent::UserAgent->new;
my $ua = AnyEvent::UserAgent->new(request_timeout => 60);

Constructor for the user agent. You can pass it either a hash or a hash reference with attribute values.

request

$ua->request(GET 'http://example.com/', sub { print($_[0]->code) });

This method will dispatch the given request object. Normally this will be an instance of the HTTP::Request class, but any object with a similar interface will do. The last argument must be a callback that will be called with a response object as first argument. Response will be an instance of the HTTP::Response class.

This method is a wrapper for the AnyEvent::HTTP::http_request() method. So you also can pass parameters for it, e.g.:

$ua->request(GET 'http://example.com/', want_body_handle => 0, sub { print($_[0]->code) });

Full parameter list see at the AnyEvent::HTTP documentation.

get

$ua->get('http://example.com/', sub { print($_[0]->code) });

This method is a wrapper for the request() method and the HTTP::Request::Common::GET() function. The last argument must be a callback.

This method is a wrapper for the request() method and the HTTP::Request::Common::HEAD() function. See get().

put

This method is a wrapper for the request() method and the HTTP::Request::Common::PUT() function. See get().

delete

This method is a wrapper for the request() method and the HTTP::Request::Common::DELETE() function. See get().

post

$ua->post('http://example.com/', [key => 'value'], sub { print($_[0]->code) });

This method is a wrapper for the request() method and the HTTP::Request::Common::POST() function. The last argument must be a callback.

SEE ALSO

AnyEvent::HTTP, HTTP::Cookies, HTTP::Request::Common, HTTP::Request, HTTP::Response.

SUPPORT

AUTHOR

Denis Ibaev dionys@cpan.org for AdCamp.ru.

CONTRIBUTORS

Andrey Khozov avkhozov@cpan.org.

This module based on original AnyEvent::HTTP::Simple module by punytan punytan@gmail.com.

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://dev.perl.org/licenses/ for more information.