HTTP-Simple
view release on metacpan or search on metacpan
lib/HTTP/Simple.pm view on Meta::CPAN
open my $fh, '<:raw', $file or croak "Failed to open $file: $!";
my %options;
$options{headers} = {'Content-Type' => $content_type} if defined $content_type;
my $chunk = 131072;
$options{content} = sub { my $buffer; sysread $fh, $buffer, $chunk; $buffer };
my $res = $UA->post($url, \%options);
return $res->{content} if $res->{success};
croak $res->{content} if $res->{status} == 599;
croak "$res->{status} $res->{reason}";
}
sub is_info { !!($_[0] >= 100 && $_[0] < 200) }
sub is_success { !!($_[0] >= 200 && $_[0] < 300) }
sub is_redirect { !!($_[0] >= 300 && $_[0] < 400) }
sub is_error { !!($_[0] >= 400 && $_[0] < 600) }
sub is_client_error { !!($_[0] >= 400 && $_[0] < 500) }
sub is_server_error { !!($_[0] >= 500 && $_[0] < 600) }
sub _load_function {
my ($module, $function) = @_;
my $code = $module->can($function);
return $code if defined $code;
(my $path = $module) =~ s{::}{/}g;
require "$path.pm";
$code = $module->can($function);
croak "'$function' not found in package $module" unless defined $code;
return $code;
}
1;
=head1 NAME
HTTP::Simple - Simple procedural interface to HTTP::Tiny
=head1 SYNOPSIS
perl -MHTTP::Simple -e'getprint(shift)' 'https://example.com'
use HTTP::Simple;
my $content = get 'https://example.com';
if (mirror('https://example.com', '/path/to/file.html') == 304) { ... }
if (is_success(getprint 'https://example.com')) { ... }
postform('https://example.com', {foo => ['bar', 'baz']});
postjson('https://example.com', [{bar => 'baz'}]);
postfile('https://example.com', '/path/to/file.png');
=head1 DESCRIPTION
This module is a wrapper of L<HTTP::Tiny> that provides simplified functions
for performing HTTP requests in a similar manner to L<LWP::Simple>, but with
slightly more useful error handling. For full control of the request process
and response handling, use L<HTTP::Tiny> directly.
L<IO::Socket::SSL> is required for HTTPS requests with L<HTTP::Tiny>.
Request methods that return the body content of the response will return a byte
string suitable for directly printing, but that may need to be
L<decoded|Encode/decode> for text operations.
The L<HTTP::Tiny> object used by these functions to make requests can be
accessed as C<$HTTP::Simple::UA> (for example, to configure the timeout, or
replace it with a compatible object like L<HTTP::Tinyish>).
The JSON encoder used by the JSON functions can be accessed as
C<$HTTP::Simple::JSON>, and defaults to a L<Cpanel::JSON::XS> object if
L<Cpanel::JSON::XS> 4.11+ is installed, and otherwise a L<JSON::PP> object. If
replaced with a new object, it should have UTF-8 encoding/decoding enabled
(usually the C<utf8> option). If it is set to a string, it will be used as a
module name that is expected to have C<decode_json> and C<encode_json>
functions.
=head1 FUNCTIONS
All functions are exported by default. Functions can also be requested
individually or with the tags C<:request>, C<:status>, or C<:all>.
=head2 get
my $contents = get($url);
Retrieves the document at the given URL with a GET request and returns it as a
byte string. Throws an exception on connection or HTTP errors.
=head2 getjson
my $data = getjson($url);
Retrieves the JSON document at the given URL with a GET request and decodes it
from JSON to a Perl structure. Throws an exception on connection, HTTP, or JSON
errors.
=head2 head
my $headers = head($url);
Retrieves the headers at the given URL with a HEAD request and returns them as
a hash reference. Header field names are normalized to lower case, and values
may be an array reference if the header is repeated. Throws an exception on
connection or HTTP errors.
=head2 getprint
my $status = getprint($url);
Retrieves the document at the given URL with a GET request and prints it as it
is received. Returns the HTTP status code. Throws an exception on connection
errors.
=head2 getstore
my $status = getstore($url, $path);
Retrieves the document at the given URL with a GET request and stores it to the
given file path. Returns the HTTP status code. Throws an exception on
( run in 0.683 second using v1.01-cache-2.11-cpan-140bd7fdf52 )