| 1 | package HTTP::Simple; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use Carp 'croak'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use File::Basename 'dirname'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use File::Temp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use HTTP::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.004'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | my @request_functions = qw(get getjson head getprint getstore mirror postform postjson postfile); | |||||||||||||||||||
| 14 | 2 | my @status_functions = qw(is_info is_success is_redirect is_error is_client_error is_server_error); | |||||||||||||||||||
| 15 | 2 | our @EXPORT = (@request_functions, @status_functions); | |||||||||||||||||||
| 16 | 2 | our %EXPORT_TAGS = ( | |||||||||||||||||||
| 17 | all => [@request_functions, @status_functions], | ||||||||||||||||||||
| 18 | request => \@request_functions, | ||||||||||||||||||||
| 19 | status => \@status_functions, | ||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 2 | our $UA = HTTP::Tiny->new(agent => "HTTP::Simple/$VERSION"); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 2 | our $JSON; | |||||||||||||||||||
| 25 | { | ||||||||||||||||||||
| 26 | 2 | local $@; | |||||||||||||||||||
| 27 | 2 | ▶ | if (eval { require Cpanel::JSON::XS; Cpanel::JSON::XS->VERSION('4.11'); 1 }) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 2 | $JSON = Cpanel::JSON::XS->new->utf8->canonical->allow_nonref->convert_blessed->allow_dupkeys; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | 2 | ▶ | unless (defined $JSON) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 0 | require JSON::PP; | |||||||||||||||||||
| 33 | 0 | $JSON = JSON::PP->new->utf8->canonical->allow_nonref->convert_blessed; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 3 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 37 | 3 | my ($url) = @_; | |||||||||||||||||||
| 38 | 3 | my $res = $UA->get($url); | |||||||||||||||||||
| 39 | 3 | ▶ | return $res->{content} if $res->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 2 | ▶ | croak $res->{content} if $res->{status} == 599; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 1 | croak "$res->{status} $res->{reason}"; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 7 | ▶ | sub getjson { | ||||||||||||||||||
|
Subroutine
getjson: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 45 | 7 | my ($url) = @_; | |||||||||||||||||||
| 46 | 7 | my $res = $UA->get($url); | |||||||||||||||||||
| 47 | 7 | ▶ | return ref($JSON) ? $JSON->decode($res->{content}) : _load_function($JSON, 'decode_json')->($res->{content}) if $res->{success}; | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 48 | 2 | ▶ | croak $res->{content} if $res->{status} == 599; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 1 | croak "$res->{status} $res->{reason}"; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 3 | ▶ | sub head { | ||||||||||||||||||
|
Subroutine
head: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 53 | 3 | my ($url) = @_; | |||||||||||||||||||
| 54 | 3 | my $res = $UA->head($url); | |||||||||||||||||||
| 55 | 3 | ▶ | return $res->{headers} if $res->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 2 | ▶ | croak $res->{content} if $res->{status} == 599; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 1 | croak "$res->{status} $res->{reason}"; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 3 | ▶ | sub getprint { | ||||||||||||||||||
|
Subroutine
getprint: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 61 | 3 | my ($url) = @_; | |||||||||||||||||||
| 62 | 3 | ▶ | my $res = $UA->get($url, {data_callback => sub { print $_[0] }}); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | 3 | ▶ | croak $res->{content} if $res->{status} == 599; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 2 | return $res->{status}; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 2 | ▶ | sub getstore { | ||||||||||||||||||
|
Subroutine
getstore: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||
| 68 | 2 | my ($url, $file) = @_; | |||||||||||||||||||
| 69 | 2 | my $temp = File::Temp->new(DIR => dirname $file); | |||||||||||||||||||
| 70 | 2 | ▶ | my $res = $UA->get($url, {data_callback => sub { print {$temp} $_[0] }}); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 71 | 2 | ▶ | croak $res->{content} if $res->{status} == 599; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 2 | ▶ | close $temp or croak "Failed to close $temp: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 2 | ▶ | rename $temp->filename, $file or croak "Failed to rename $temp to $file: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 2 | $temp->unlink_on_destroy(0); | |||||||||||||||||||
| 75 | 2 | return $res->{status}; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 3 | ▶ | sub mirror { | ||||||||||||||||||
|
Subroutine
mirror: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 79 | 3 | my ($url, $file) = @_; | |||||||||||||||||||
| 80 | 3 | my $res = $UA->mirror($url, $file); | |||||||||||||||||||
| 81 | 3 | ▶ | return $res->{status} if $res->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 2 | ▶ | croak $res->{content} if $res->{status} == 599; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 1 | croak "$res->{status} $res->{reason}"; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 3 | ▶ | sub postform { | ||||||||||||||||||
|
Subroutine
postform: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 87 | 3 | my ($url, $form) = @_; | |||||||||||||||||||
| 88 | 3 | my $res = $UA->post_form($url, $form); | |||||||||||||||||||
| 89 | 3 | ▶ | return $res->{content} if $res->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 2 | ▶ | croak $res->{content} if $res->{status} == 599; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 1 | croak "$res->{status} $res->{reason}"; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 4 | ▶ | sub postjson { | ||||||||||||||||||
|
Subroutine
postjson: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 95 | 4 | my ($url, $data) = @_; | |||||||||||||||||||
| 96 | 4 | my %options; | |||||||||||||||||||
| 97 | 4 | $options{headers} = {'Content-Type' => 'application/json; charset=UTF-8'}; | |||||||||||||||||||
| 98 | 4 | ▶ | $options{content} = ref($JSON) ? $JSON->encode($data) : _load_function($JSON, 'encode_json')->($data); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 4 | my $res = $UA->post($url, \%options); | |||||||||||||||||||
| 100 | 4 | ▶ | return $res->{content} if $res->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 2 | ▶ | croak $res->{content} if $res->{status} == 599; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 1 | croak "$res->{status} $res->{reason}"; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 5 | ▶ | sub postfile { | ||||||||||||||||||
|
Subroutine
postfile: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 106 | 5 | my ($url, $file, $content_type) = @_; | |||||||||||||||||||
| 107 | 5 | ▶ | open my $fh, '<:raw', $file or croak "Failed to open $file: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 5 | my %options; | |||||||||||||||||||
| 109 | 5 | ▶ | $options{headers} = {'Content-Type' => $content_type} if defined $content_type; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 5 | my $chunk = 131072; | |||||||||||||||||||
| 111 | 5 | ▶ | $options{content} = sub { my $buffer; sysread $fh, $buffer, $chunk; $buffer }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 112 | 5 | my $res = $UA->post($url, \%options); | |||||||||||||||||||
| 113 | 5 | ▶ | return $res->{content} if $res->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 2 | ▶ | croak $res->{content} if $res->{status} == 599; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 1 | croak "$res->{status} $res->{reason}"; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 104 | ▶ | sub is_info { !!($_[0] >= 100 && $_[0] < 200) } | ||||||||||||||||||
|
Subroutine
is_info: called CC=2 SCAR=6.9
Pod
documented
Condition100%3/3
$_[0] >= 100 && $_[0] < 200
MC/DC100%2/2
$_[0] >= 100 && $_[0] < 200
$_[0] >= 100
$_[0] < 200
Truth table100%3/3
A && B
A= $_[0] >= 100B= $_[0] < 200
| |||||||||||||||||||||
| 119 | 104 | ▶ | sub is_success { !!($_[0] >= 200 && $_[0] < 300) } | ||||||||||||||||||
|
Subroutine
is_success: called CC=2 SCAR=6.9
Pod
documented
Condition100%3/3
$_[0] >= 200 && $_[0] < 300
MC/DC100%2/2
$_[0] >= 200 && $_[0] < 300
$_[0] >= 200
$_[0] < 300
Truth table100%3/3
A && B
A= $_[0] >= 200B= $_[0] < 300
| |||||||||||||||||||||
| 120 | 104 | ▶ | sub is_redirect { !!($_[0] >= 300 && $_[0] < 400) } | ||||||||||||||||||
|
Subroutine
is_redirect: called CC=2 SCAR=6.9
Pod
documented
Condition100%3/3
$_[0] >= 300 && $_[0] < 400
MC/DC100%2/2
$_[0] >= 300 && $_[0] < 400
$_[0] >= 300
$_[0] < 400
Truth table100%3/3
A && B
A= $_[0] >= 300B= $_[0] < 400
| |||||||||||||||||||||
| 121 | 205 | ▶ | sub is_error { !!($_[0] >= 400 && $_[0] < 600) } | ||||||||||||||||||
|
Subroutine
is_error: called CC=2 SCAR=6.9
Pod
documented
Condition100%3/3
$_[0] >= 400 && $_[0] < 600
MC/DC100%2/2
$_[0] >= 400 && $_[0] < 600
$_[0] >= 400
$_[0] < 600
Truth table100%3/3
A && B
A= $_[0] >= 400B= $_[0] < 600
| |||||||||||||||||||||
| 122 | 104 | ▶ | sub is_client_error { !!($_[0] >= 400 && $_[0] < 500) } | ||||||||||||||||||
|
Subroutine
is_client_error: called CC=2 SCAR=6.9
Pod
documented
Condition100%3/3
$_[0] >= 400 && $_[0] < 500
MC/DC100%2/2
$_[0] >= 400 && $_[0] < 500
$_[0] >= 400
$_[0] < 500
Truth table100%3/3
A && B
A= $_[0] >= 400B= $_[0] < 500
| |||||||||||||||||||||
| 123 | 104 | ▶ | sub is_server_error { !!($_[0] >= 500 && $_[0] < 600) } | ||||||||||||||||||
|
Subroutine
is_server_error: called CC=2 SCAR=6.9
Pod
documented
Condition100%3/3
$_[0] >= 500 && $_[0] < 600
MC/DC100%2/2
$_[0] >= 500 && $_[0] < 600
$_[0] >= 500
$_[0] < 600
Truth table100%3/3
A && B
A= $_[0] >= 500B= $_[0] < 600
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 5 | ▶ | sub _load_function { | ||||||||||||||||||
|
Subroutine
_load_function: called CC=3 SCAR=13.0
| |||||||||||||||||||||
| 126 | 5 | my ($module, $function) = @_; | |||||||||||||||||||
| 127 | 5 | my $code = $module->can($function); | |||||||||||||||||||
| 128 | 5 | ▶ | return $code if defined $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 2 | (my $path = $module) =~ s{::}{/}g; | |||||||||||||||||||
| 130 | 2 | require "$path.pm"; | |||||||||||||||||||
| 131 | 0 | $code = $module->can($function); | |||||||||||||||||||
| 132 | 0 | ▶ | croak "'$function' not found in package $module" unless defined $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 0 | return $code; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 2 | 1; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =head1 NAME | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | HTTP::Simple - Simple procedural interface to HTTP::Tiny | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =head1 SYNOPSIS | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | perl -MHTTP::Simple -e'getprint(shift)' 'https://example.com' | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | use HTTP::Simple; | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | my $content = get 'https://example.com'; | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | if (mirror('https://example.com', '/path/to/file.html') == 304) { ... } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | if (is_success(getprint 'https://example.com')) { ... } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | postform('https://example.com', {foo => ['bar', 'baz']}); | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | postjson('https://example.com', [{bar => 'baz'}]); | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | postfile('https://example.com', '/path/to/file.png'); | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =head1 DESCRIPTION | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | This module is a wrapper of L<HTTP::Tiny> that provides simplified functions | ||||||||||||||||||||
| 163 | for performing HTTP requests in a similar manner to L<LWP::Simple>, but with | ||||||||||||||||||||
| 164 | slightly more useful error handling. For full control of the request process | ||||||||||||||||||||
| 165 | and response handling, use L<HTTP::Tiny> directly. | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | L<IO::Socket::SSL> is required for HTTPS requests with L<HTTP::Tiny>. | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | Request methods that return the body content of the response will return a byte | ||||||||||||||||||||
| 170 | string suitable for directly printing, but that may need to be | ||||||||||||||||||||
| 171 | L<decoded|Encode/decode> for text operations. | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | The L<HTTP::Tiny> object used by these functions to make requests can be | ||||||||||||||||||||
| 174 | accessed as C<$HTTP::Simple::UA> (for example, to configure the timeout, or | ||||||||||||||||||||
| 175 | replace it with a compatible object like L<HTTP::Tinyish>). | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | The JSON encoder used by the JSON functions can be accessed as | ||||||||||||||||||||
| 178 | C<$HTTP::Simple::JSON>, and defaults to a L<Cpanel::JSON::XS> object if | ||||||||||||||||||||
| 179 | L<Cpanel::JSON::XS> 4.11+ is installed, and otherwise a L<JSON::PP> object. If | ||||||||||||||||||||
| 180 | replaced with a new object, it should have UTF-8 encoding/decoding enabled | ||||||||||||||||||||
| 181 | (usually the C<utf8> option). If it is set to a string, it will be used as a | ||||||||||||||||||||
| 182 | module name that is expected to have C<decode_json> and C<encode_json> | ||||||||||||||||||||
| 183 | functions. | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | =head1 FUNCTIONS | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | All functions are exported by default. Functions can also be requested | ||||||||||||||||||||
| 188 | individually or with the tags C<:request>, C<:status>, or C<:all>. | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =head2 get | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | my $contents = get($url); | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | Retrieves the document at the given URL with a GET request and returns it as a | ||||||||||||||||||||
| 195 | byte string. Throws an exception on connection or HTTP errors. | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =head2 getjson | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | my $data = getjson($url); | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | Retrieves the JSON document at the given URL with a GET request and decodes it | ||||||||||||||||||||
| 202 | from JSON to a Perl structure. Throws an exception on connection, HTTP, or JSON | ||||||||||||||||||||
| 203 | errors. | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =head2 head | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | my $headers = head($url); | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | Retrieves the headers at the given URL with a HEAD request and returns them as | ||||||||||||||||||||
| 210 | a hash reference. Header field names are normalized to lower case, and values | ||||||||||||||||||||
| 211 | may be an array reference if the header is repeated. Throws an exception on | ||||||||||||||||||||
| 212 | connection or HTTP errors. | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =head2 getprint | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | my $status = getprint($url); | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | Retrieves the document at the given URL with a GET request and prints it as it | ||||||||||||||||||||
| 219 | is received. Returns the HTTP status code. Throws an exception on connection | ||||||||||||||||||||
| 220 | errors. | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | =head2 getstore | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | my $status = getstore($url, $path); | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | Retrieves the document at the given URL with a GET request and stores it to the | ||||||||||||||||||||
| 227 | given file path. Returns the HTTP status code. Throws an exception on | ||||||||||||||||||||
| 228 | connection or filesystem errors. | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | =head2 mirror | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | my $status = mirror($url, $path); | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | Retrieves the document at the given URL with a GET request and mirrors it to | ||||||||||||||||||||
| 235 | the given file path, using the C<If-Modified-Since> headers to short-circuit if | ||||||||||||||||||||
| 236 | the file exists and is new enough, and the C<Last-Modified> header to set its | ||||||||||||||||||||
| 237 | modification time. Returns the HTTP status code. Throws an exception on | ||||||||||||||||||||
| 238 | connection, HTTP, or filesystem errors. | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | =head2 postform | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | my $contents = postform($url, $form); | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | Sends a POST request to the given URL with the given hash or array reference of | ||||||||||||||||||||
| 245 | form data serialized to C<application/x-www-form-urlencoded>. Returns the | ||||||||||||||||||||
| 246 | response body as a byte string. Throws an exception on connection or HTTP | ||||||||||||||||||||
| 247 | errors. | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | =head2 postjson | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | my $contents = postjson($url, $data); | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | Sends a POST request to the given URL with the given data structure encoded to | ||||||||||||||||||||
| 254 | JSON. Returns the response body as a byte string. Throws an exception on | ||||||||||||||||||||
| 255 | connection, HTTP, or JSON errors. | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | =head2 postfile | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | my $contents = postfile($url, $path); | ||||||||||||||||||||
| 260 | my $contents = postfile($url, $path, $content_type); | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | Sends a POST request to the given URL, streaming the contents of the given | ||||||||||||||||||||
| 263 | file. The content type is passed as C<application/octet-stream> if not | ||||||||||||||||||||
| 264 | specified. Returns the response body as a byte string. Throws an exception on | ||||||||||||||||||||
| 265 | connection, HTTP, or filesystem errors. | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | =head2 is_info | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | my $bool = is_info($status); | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | Returns true if the status code indicates an informational response (C<1xx>). | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | =head2 is_success | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | my $bool = is_success($status); | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | Returns true if the status code indicates a successful response (C<2xx>). | ||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | =head2 is_redirect | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | my $bool = is_redirect($status); | ||||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | Returns true if the status code indicates a redirection response (C<3xx>). | ||||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | =head2 is_error | ||||||||||||||||||||
| 286 | |||||||||||||||||||||
| 287 | my $bool = is_error($status); | ||||||||||||||||||||
| 288 | |||||||||||||||||||||
| 289 | Returns true if the status code indicates an error response (C<4xx> or C<5xx>). | ||||||||||||||||||||
| 290 | |||||||||||||||||||||
| 291 | =head2 is_client_error | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | my $bool = is_client_error($status); | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | Returns true if the status code indicates a client error response (C<4xx>). | ||||||||||||||||||||
| 296 | |||||||||||||||||||||
| 297 | =head2 is_server_error | ||||||||||||||||||||
| 298 | |||||||||||||||||||||
| 299 | my $bool = is_server_error($status); | ||||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | Returns true if the status code indicates a server error response (C<5xx>). | ||||||||||||||||||||
| 302 | |||||||||||||||||||||
| 303 | =head1 BUGS | ||||||||||||||||||||
| 304 | |||||||||||||||||||||
| 305 | Report any issues on the public bugtracker. | ||||||||||||||||||||
| 306 | |||||||||||||||||||||
| 307 | =head1 AUTHOR | ||||||||||||||||||||
| 308 | |||||||||||||||||||||
| 309 | Dan Book <dbook@cpan.org> | ||||||||||||||||||||
| 310 | |||||||||||||||||||||
| 311 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 312 | |||||||||||||||||||||
| 313 | This software is Copyright (c) 2019 by Dan Book. | ||||||||||||||||||||
| 314 | |||||||||||||||||||||
| 315 | This is free software, licensed under: | ||||||||||||||||||||
| 316 | |||||||||||||||||||||
| 317 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||
| 318 | |||||||||||||||||||||
| 319 | =head1 SEE ALSO | ||||||||||||||||||||
| 320 | |||||||||||||||||||||
| 321 | L<LWP::Simple>, L<HTTP::Tinyish>, L<ojo> | ||||||||||||||||||||