1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
[](https://travis-ci.org/Corion/Future-HTTP)
[](https://ci.appveyor.com/project/Corion/Future-HTTP)
# NAME
Future::HTTP - provide the most appropriate HTTP client with a Future API
# SYNOPSIS
my $ua = Future::HTTP->new();
my $res = $ua->http_get('http://www.nethype.de/')->then(sub {
my( $body, $data ) = @_;
# ... handle the response
return $body
})->get();
This module is a wrapper combining [Future](https://metacpan.org/pod/Future) with the API provided
by [AnyEvent::HTTP](https://metacpan.org/pod/AnyEvent%3A%3AHTTP). The backend used for the HTTP protocols
depends on whether one of the event loops is loaded.
## Supported event loops
Currently, the following backends are supported:
- [HTTP::Tiny](https://metacpan.org/pod/HTTP%3A%3ATiny)
- [HTTP::Tiny::Paranoid](https://metacpan.org/pod/HTTP%3A%3ATiny%3A%3AParanoid)
- [Mojolicious](https://metacpan.org/pod/Mojolicious)
- [AnyEvent](https://metacpan.org/pod/AnyEvent)
- [IO::Async](https://metacpan.org/pod/IO%3A%3AAsync)
Support
is planned for [LWP::UserAgent](https://metacpan.org/pod/LWP%3A%3AUserAgent) and [POE](https://metacpan.org/pod/POE)
but has not materialized yet.
# METHODS
## `Future::HTTP->new()`
my $ua = Future::HTTP->new();
Creates a new instance of the HTTP client.
## `$ua->is_async()`
Returns true if the selected backend is asynchronous, false if it is
synchronous.
## `$ua->http_get($url, %options)`
my $res = $ua->http_get('http://example.com/',
headers => {
'Accept' => 'text/json',
},
)->then(sub {
my( $body, $headers ) = @_;
# ... handle the response
})->get;
Retrieves the URL and returns the body and headers, like
the function in [AnyEvent::HTTP](https://metacpan.org/pod/AnyEvent%3A%3AHTTP).
## `$ua->http_head($url, %options)`
my $res = $ua->http_head('http://example.com/',
headers => {
'Accept' => 'text/json',
},
)->then(sub {
my( $body, $headers ) = @_;
...
})->get;
Retrieves the header of the URL and returns the headers,
like the function in [AnyEvent::HTTP](https://metacpan.org/pod/AnyEvent%3A%3AHTTP).
## `$ua->http_post($url, $body, %options)`
my $res = $ua->http_post('http://example.com/api',
'{token:"my_json_token"}',
headers => {
'Accept' => 'text/json',
},
)->then(sub {
my( $body, $headers ) = @_;
...
})->get;
Posts the content to the URL and returns the body and headers,
like the function in [AnyEvent::HTTP](https://metacpan.org/pod/AnyEvent%3A%3AHTTP).
## `$ua->http_request($method, $url, %options)`
my $res = $ua->http_request('PUT' => 'http://example.com/api',
headers => {
'Accept' => 'text/json',
},
body => '{token:"my_json_token"}',
)->then(sub {
my( $body, $headers ) = @_;
...
})->get;
Posts the content to the URL and returns the body and headers,
like the function in [AnyEvent::HTTP](https://metacpan.org/pod/AnyEvent%3A%3AHTTP).
# SEE ALSO
[Future](https://metacpan.org/pod/Future)
[AnyEvent::HTTP](https://metacpan.org/pod/AnyEvent%3A%3AHTTP) for the details of the API
# REPOSITORY
The public repository of this module is
[https://github.com/Corion/future-http](https://github.com/Corion/future-http).
# SUPPORT
The public support forum of this module is
[https://perlmonks.org/](https://perlmonks.org/).
# BUG TRACKER
Please report bugs in this module via the RT CPAN bug queue at
[https://rt.cpan.org/Public/Dist/Display.html?Name=Future-HTTP](https://rt.cpan.org/Public/Dist/Display.html?Name=Future-HTTP)
or via mail to [future-http-Bugs@rt.cpan.org](https://metacpan.org/pod/future-http-Bugs%40rt.cpan.org).
# AUTHOR
Max Maischein `corion@cpan.org`
# COPYRIGHT (c)
Copyright 2016-2024 by Max Maischein `corion@cpan.org`.
# LICENSE
This module is released under the same terms as Perl itself.
|