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

Net::Async::HTTP - Asynchronous HTTP user agent

SYNOPSIS

 use IO::Async::Loop::...;
 use Net::Async::HTTP;
 use URI;

 my $loop = IO::Async::Loop::...;

 my $http = Net::Async::HTTP->new( loop => $loop );

 $http->do_request(
    uri => URI->new( "http://www.cpan.org/" ),

    on_response => sub {
       my ( $response ) = @_;
       print "Front page of http://www.cpan.org/ is:\n";
       print $response->as_string;
       $loop->loop_stop;
    },

    on_error => sub {
       my ( $message ) = @_;
       print "Cannot fetch http://www.cpan.org/ - $message\n";
       $loop->loop_stop;
    },
 );

 $loop->loop_forever;

DESCRIPTION

This object class implements an asynchronous HTTP user agent. It sends requests to servers, and invokes continuation callbacks when responses are received. The object supports multiple concurrent connections to servers, and allows multiple outstanding requests in pipeline to any one connection. Normally, only one such object will be needed per program to support any number of requests.

CONSTRUCTOR

$http = Net::Async::HTTP->new( %args )

This function returns a new instance of a Net::Async::HTTP object. It takes the following named arguments:

loop => IO::Async::Loop

A reference to an IO::Async::Loop object.

user_agent => STRING

A string to set in the User-Agent HTTP header. If not supplied, one will be constructed that declares Net::Async::HTTP and the version number.

METHODS

$http->do_request( %args )

Send an HTTP request to a server, and set up the callbacks to receive a reply. The request may be represented by an HTTP::Request object, or a URI object, depending on the arguments passed.

The following named arguments are used for HTTP::Requests:

request => HTTP::Request

A reference to an HTTP::Request object

host => STRING
port => INT

Hostname and port number of the server to connect to

The following named arguments are used for URI requests:

uri => URI

A reference to a URI object

method => STRING

Optional. The HTTP method. If missing, GET is used.

content => STRING

Optional. The body content to use for POST requests.

user => STRING
pass => STRING

Optional. If both are given, the HTTP Basic Authorization header will be sent with these details.

proxy_host => STRING
proxy_port => INT

Optional. Override the hostname or port number implied by the URI.

For either request type, it takes the following continuation callbacks:

on_response => CODE

A callback that is invoked when a response to this request has been received. It will be passed an HTTP::Response object containing the response the server sent.

 $on_response->( $response )
on_error => CODE

A callback that is invoked if an error occurs while trying to send the request or obtain the response. It will be passed an error message.

 $on_error->( $message )

SEE ALSO

  • RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>