1HTTP::Tiny(3)         User Contributed Perl Documentation        HTTP::Tiny(3)
2
3
4

NAME

6       HTTP::Tiny - A small, simple, correct HTTP/1.1 client
7

VERSION

9       version 0.080
10

SYNOPSIS

12           use HTTP::Tiny;
13
14           my $response = HTTP::Tiny->new->get('http://example.com/');
15
16           die "Failed!\n" unless $response->{success};
17
18           print "$response->{status} $response->{reason}\n";
19
20           while (my ($k, $v) = each %{$response->{headers}}) {
21               for (ref $v eq 'ARRAY' ? @$v : $v) {
22                   print "$k: $_\n";
23               }
24           }
25
26           print $response->{content} if length $response->{content};
27

DESCRIPTION

29       This is a very simple HTTP/1.1 client, designed for doing simple
30       requests without the overhead of a large framework like LWP::UserAgent.
31
32       It is more correct and more complete than HTTP::Lite.  It supports
33       proxies and redirection.  It also correctly resumes after EINTR.
34
35       If IO::Socket::IP 0.25 or later is installed, HTTP::Tiny will use it
36       instead of IO::Socket::INET for transparent support for both IPv4 and
37       IPv6.
38
39       Cookie support requires HTTP::CookieJar or an equivalent class.
40

METHODS

42   new
43           $http = HTTP::Tiny->new( %attributes );
44
45       This constructor returns a new HTTP::Tiny object.  Valid attributes
46       include:
47
48       •   "agent" — A user-agent string (defaults to 'HTTP-Tiny/$VERSION').
49           If "agent" — ends in a space character, the default user-agent
50           string is appended.
51
52       •   "cookie_jar" — An instance of HTTP::CookieJar — or equivalent class
53           that supports the "add" and "cookie_header" methods
54
55       •   "default_headers" — A hashref of default headers to apply to
56           requests
57
58       •   "local_address" — The local IP address to bind to
59
60       •   "keep_alive" — Whether to reuse the last connection (if for the
61           same scheme, host and port) (defaults to 1)
62
63       •   "max_redirect" — Maximum number of redirects allowed (defaults to
64           5)
65
66       •   "max_size" — Maximum response size in bytes (only when not using a
67           data callback).  If defined, requests with responses larger than
68           this will return a 599 status code.
69
70       •   "http_proxy" — URL of a proxy server to use for HTTP connections
71           (default is $ENV{http_proxy} — if set)
72
73       •   "https_proxy" — URL of a proxy server to use for HTTPS connections
74           (default is $ENV{https_proxy} — if set)
75
76       •   "proxy" — URL of a generic proxy server for both HTTP and HTTPS
77           connections (default is $ENV{all_proxy} — if set)
78
79       •   "no_proxy" — List of domain suffixes that should not be proxied.
80           Must be a comma-separated string or an array reference. (default is
81           $ENV{no_proxy} —)
82
83       •   "timeout" — Request timeout in seconds (default is 60) If a socket
84           open, read or write takes longer than the timeout, the request
85           response status code will be 599.
86
87       •   "verify_SSL" — A boolean that indicates whether to validate the SSL
88           certificate of an "https" — connection (default is false)
89
90       •   "SSL_options" — A hashref of "SSL_*" — options to pass through to
91           IO::Socket::SSL
92
93       An accessor/mutator method exists for each attribute.
94
95       Passing an explicit "undef" for "proxy", "http_proxy" or "https_proxy"
96       will prevent getting the corresponding proxies from the environment.
97
98       Errors during request execution will result in a pseudo-HTTP status
99       code of 599 and a reason of "Internal Exception". The content field in
100       the response will contain the text of the error.
101
102       The "keep_alive" parameter enables a persistent connection, but only to
103       a single destination scheme, host and port.  If any connection-relevant
104       attributes are modified via accessor, or if the process ID or thread ID
105       change, the persistent connection will be dropped.  If you want
106       persistent connections across multiple destinations, use multiple
107       HTTP::Tiny objects.
108
109       See "SSL SUPPORT" for more on the "verify_SSL" and "SSL_options"
110       attributes.
111
112   get|head|put|post|patch|delete
113           $response = $http->get($url);
114           $response = $http->get($url, \%options);
115           $response = $http->head($url);
116
117       These methods are shorthand for calling "request()" for the given
118       method.  The URL must have unsafe characters escaped and international
119       domain names encoded.  See "request()" for valid options and a
120       description of the response.
121
122       The "success" field of the response will be true if the status code is
123       2XX.
124
125   post_form
126           $response = $http->post_form($url, $form_data);
127           $response = $http->post_form($url, $form_data, \%options);
128
129       This method executes a "POST" request and sends the key/value pairs
130       from a form data hash or array reference to the given URL with a
131       "content-type" of "application/x-www-form-urlencoded".  If data is
132       provided as an array reference, the order is preserved; if provided as
133       a hash reference, the terms are sorted on key and value for
134       consistency.  See documentation for the "www_form_urlencode" method for
135       details on the encoding.
136
137       The URL must have unsafe characters escaped and international domain
138       names encoded.  See "request()" for valid options and a description of
139       the response.  Any "content-type" header or content in the options
140       hashref will be ignored.
141
142       The "success" field of the response will be true if the status code is
143       2XX.
144
145   mirror
146           $response = $http->mirror($url, $file, \%options)
147           if ( $response->{success} ) {
148               print "$file is up to date\n";
149           }
150
151       Executes a "GET" request for the URL and saves the response body to the
152       file name provided.  The URL must have unsafe characters escaped and
153       international domain names encoded.  If the file already exists, the
154       request will include an "If-Modified-Since" header with the
155       modification timestamp of the file.  You may specify a different
156       "If-Modified-Since" header yourself in the "$options->{headers}" hash.
157
158       The "success" field of the response will be true if the status code is
159       2XX or if the status code is 304 (unmodified).
160
161       If the file was modified and the server response includes a properly
162       formatted "Last-Modified" header, the file modification time will be
163       updated accordingly.
164
165   request
166           $response = $http->request($method, $url);
167           $response = $http->request($method, $url, \%options);
168
169       Executes an HTTP request of the given method type ('GET', 'HEAD',
170       'POST', 'PUT', etc.) on the given URL.  The URL must have unsafe
171       characters escaped and international domain names encoded.
172
173       NOTE: Method names are case-sensitive per the HTTP/1.1 specification.
174       Don't use "get" when you really want "GET".  See LIMITATIONS for how
175       this applies to redirection.
176
177       If the URL includes a "user:password" stanza, they will be used for
178       Basic-style authorization headers.  (Authorization headers will not be
179       included in a redirected request.) For example:
180
181           $http->request('GET', 'http://Aladdin:open sesame@example.com/');
182
183       If the "user:password" stanza contains reserved characters, they must
184       be percent-escaped:
185
186           $http->request('GET', 'http://john%40example.com:password@example.com/');
187
188       A hashref of options may be appended to modify the request.
189
190       Valid options are:
191
192       •   "headers" — A hashref containing headers to include with the
193           request.  If the value for a header is an array reference, the
194           header will be output multiple times with each value in the array.
195           These headers over-write any default headers.
196
197       •   "content" — A scalar to include as the body of the request OR a
198           code reference that will be called iteratively to produce the body
199           of the request
200
201       •   "trailer_callback" — A code reference that will be called if it
202           exists to provide a hashref of trailing headers (only used with
203           chunked transfer-encoding)
204
205       •   "data_callback" — A code reference that will be called for each
206           chunks of the response body received.
207
208       •   "peer" — Override host resolution and force all connections to go
209           only to a specific peer address, regardless of the URL of the
210           request.  This will include any redirections!  This options should
211           be used with extreme caution (e.g. debugging or very special
212           circumstances). It can be given as either a scalar or a code
213           reference that will receive the hostname and whose response will be
214           taken as the address.
215
216       The "Host" header is generated from the URL in accordance with RFC
217       2616.  It is a fatal error to specify "Host" in the "headers" option.
218       Other headers may be ignored or overwritten if necessary for transport
219       compliance.
220
221       If the "content" option is a code reference, it will be called
222       iteratively to provide the content body of the request.  It should
223       return the empty string or undef when the iterator is exhausted.
224
225       If the "content" option is the empty string, no "content-type" or
226       "content-length" headers will be generated.
227
228       If the "data_callback" option is provided, it will be called
229       iteratively until the entire response body is received.  The first
230       argument will be a string containing a chunk of the response body, the
231       second argument will be the in-progress response hash reference, as
232       described below.  (This allows customizing the action of the callback
233       based on the "status" or "headers" received prior to the content body.)
234
235       The "request" method returns a hashref containing the response.  The
236       hashref will have the following keys:
237
238       •   "success" — Boolean indicating whether the operation returned a 2XX
239           status code
240
241       •   "url" — URL that provided the response. This is the URL of the
242           request unless there were redirections, in which case it is the
243           last URL queried in a redirection chain
244
245       •   "status" — The HTTP status code of the response
246
247       •   "reason" — The response phrase returned by the server
248
249       •   "content" — The body of the response.  If the response does not
250           have any content or if a data callback is provided to consume the
251           response body, this will be the empty string
252
253       •   "headers" — A hashref of header fields.  All header field names
254           will be normalized to be lower case. If a header is repeated, the
255           value will be an arrayref; it will otherwise be a scalar string
256           containing the value
257
258       •   "protocol" - If this field exists, it is the protocol of the
259           response such as HTTP/1.0 or HTTP/1.1
260
261       •   "redirects" If this field exists, it is an arrayref of response
262           hash references from redirects in the same order that redirections
263           occurred.  If it does not exist, then no redirections occurred.
264
265       On an error during the execution of the request, the "status" field
266       will contain 599, and the "content" field will contain the text of the
267       error.
268
269   www_form_urlencode
270           $params = $http->www_form_urlencode( $data );
271           $response = $http->get("http://example.com/query?$params");
272
273       This method converts the key/value pairs from a data hash or array
274       reference into a "x-www-form-urlencoded" string.  The keys and values
275       from the data reference will be UTF-8 encoded and escaped per RFC 3986.
276       If a value is an array reference, the key will be repeated with each of
277       the values of the array reference.  If data is provided as a hash
278       reference, the key/value pairs in the resulting string will be sorted
279       by key and value for consistent ordering.
280
281   can_ssl
282           $ok         = HTTP::Tiny->can_ssl;
283           ($ok, $why) = HTTP::Tiny->can_ssl;
284           ($ok, $why) = $http->can_ssl;
285
286       Indicates if SSL support is available.  When called as a class object,
287       it checks for the correct version of Net::SSLeay and IO::Socket::SSL.
288       When called as an object methods, if "SSL_verify" is true or if
289       "SSL_verify_mode" is set in "SSL_options", it checks that a CA file is
290       available.
291
292       In scalar context, returns a boolean indicating if SSL is available.
293       In list context, returns the boolean and a (possibly multi-line) string
294       of errors indicating why SSL isn't available.
295
296   connected
297           $host = $http->connected;
298           ($host, $port) = $http->connected;
299
300       Indicates if a connection to a peer is being kept alive, per the
301       "keep_alive" option.
302
303       In scalar context, returns the peer host and port, joined with a colon,
304       or "undef" (if no peer is connected).  In list context, returns the
305       peer host and port or an empty list (if no peer is connected).
306
307       Note: This method cannot reliably be used to discover whether the
308       remote host has closed its end of the socket.
309

SSL SUPPORT

311       Direct "https" connections are supported only if IO::Socket::SSL 1.56
312       or greater and Net::SSLeay 1.49 or greater are installed. An error will
313       occur if new enough versions of these modules are not installed or if
314       the SSL encryption fails. You can also use "HTTP::Tiny::can_ssl()"
315       utility function that returns boolean to see if the required modules
316       are installed.
317
318       An "https" connection may be made via an "http" proxy that supports the
319       CONNECT command (i.e. RFC 2817).  You may not proxy "https" via a proxy
320       that itself requires "https" to communicate.
321
322       SSL provides two distinct capabilities:
323
324       •   Encrypted communication channel
325
326       •   Verification of server identity
327
328       By default, HTTP::Tiny does not verify server identity.
329
330       Server identity verification is controversial and potentially tricky
331       because it depends on a (usually paid) third-party Certificate
332       Authority (CA) trust model to validate a certificate as legitimate.
333       This discriminates against servers with self-signed certificates or
334       certificates signed by free, community-driven CA's such as CAcert.org
335       <http://cacert.org>.
336
337       By default, HTTP::Tiny does not make any assumptions about your trust
338       model, threat level or risk tolerance.  It just aims to give you an
339       encrypted channel when you need one.
340
341       Setting the "verify_SSL" attribute to a true value will make HTTP::Tiny
342       verify that an SSL connection has a valid SSL certificate corresponding
343       to the host name of the connection and that the SSL certificate has
344       been verified by a CA.  Assuming you trust the CA, this will protect
345       against a man-in-the-middle attack <http://en.wikipedia.org/wiki/Man-
346       in-the-middle_attack>.  If you are concerned about security, you should
347       enable this option.
348
349       Certificate verification requires a file containing trusted CA
350       certificates.
351
352       If the environment variable "SSL_CERT_FILE" is present, HTTP::Tiny will
353       try to find a CA certificate file in that location.
354
355       If the Mozilla::CA module is installed, HTTP::Tiny will use the CA file
356       included with it as a source of trusted CA's.  (This means you trust
357       Mozilla, the author of Mozilla::CA, the CPAN mirror where you got
358       Mozilla::CA, the toolchain used to install it, and your operating
359       system security, right?)
360
361       If that module is not available, then HTTP::Tiny will search several
362       system-specific default locations for a CA certificate file:
363
364       •   /etc/ssl/certs/ca-certificates.crt
365
366       •   /etc/pki/tls/certs/ca-bundle.crt
367
368       •   /etc/ssl/ca-bundle.pem
369
370       An error will be occur if "verify_SSL" is true and no CA certificate
371       file is available.
372
373       If you desire complete control over SSL connections, the "SSL_options"
374       attribute lets you provide a hash reference that will be passed through
375       to "IO::Socket::SSL::start_SSL()", overriding any options set by
376       HTTP::Tiny. For example, to provide your own trusted CA file:
377
378           SSL_options => {
379               SSL_ca_file => $file_path,
380           }
381
382       The "SSL_options" attribute could also be used for such things as
383       providing a client certificate for authentication to a server or
384       controlling the choice of cipher used for the SSL connection. See
385       IO::Socket::SSL documentation for details.
386

PROXY SUPPORT

388       HTTP::Tiny can proxy both "http" and "https" requests.  Only Basic
389       proxy authorization is supported and it must be provided as part of the
390       proxy URL: "http://user:pass@proxy.example.com/".
391
392       HTTP::Tiny supports the following proxy environment variables:
393
394       •   http_proxy or HTTP_PROXY
395
396       •   https_proxy or HTTPS_PROXY
397
398       •   all_proxy or ALL_PROXY
399
400       If the "REQUEST_METHOD" environment variable is set, then this might be
401       a CGI process and "HTTP_PROXY" would be set from the "Proxy:" header,
402       which is a security risk.  If "REQUEST_METHOD" is set, "HTTP_PROXY"
403       (the upper case variant only) is ignored, but "CGI_HTTP_PROXY" is
404       considered instead.
405
406       Tunnelling "https" over an "http" proxy using the CONNECT method is
407       supported.  If your proxy uses "https" itself, you can not tunnel
408       "https" over it.
409
410       Be warned that proxying an "https" connection opens you to the risk of
411       a man-in-the-middle attack by the proxy server.
412
413       The "no_proxy" environment variable is supported in the format of a
414       comma-separated list of domain extensions proxy should not be used for.
415
416       Proxy arguments passed to "new" will override their corresponding
417       environment variables.
418

LIMITATIONS

420       HTTP::Tiny is conditionally compliant with the HTTP/1.1 specifications
421       <http://www.w3.org/Protocols/>:
422
423       •   "Message Syntax and Routing" [RFC7230]
424
425       •   "Semantics and Content" [RFC7231]
426
427       •   "Conditional Requests" [RFC7232]
428
429       •   "Range Requests" [RFC7233]
430
431       •   "Caching" [RFC7234]
432
433       •   "Authentication" [RFC7235]
434
435       It attempts to meet all "MUST" requirements of the specification, but
436       does not implement all "SHOULD" requirements.  (Note: it was developed
437       against the earlier RFC 2616 specification and may not yet meet the
438       revised RFC 7230-7235 spec.) Additionally, HTTP::Tiny supports the
439       "PATCH" method of RFC 5789.
440
441       Some particular limitations of note include:
442
443       •   HTTP::Tiny focuses on correct transport.  Users are responsible for
444           ensuring that user-defined headers and content are compliant with
445           the HTTP/1.1 specification.
446
447       •   Users must ensure that URLs are properly escaped for unsafe
448           characters and that international domain names are properly encoded
449           to ASCII. See URI::Escape, URI::_punycode and Net::IDN::Encode.
450
451       •   Redirection is very strict against the specification.  Redirection
452           is only automatic for response codes 301, 302, 307 and 308 if the
453           request method is 'GET' or 'HEAD'.  Response code 303 is always
454           converted into a 'GET' redirection, as mandated by the
455           specification.  There is no automatic support for status 305 ("Use
456           proxy") redirections.
457
458       •   There is no provision for delaying a request body using an "Expect"
459           header.  Unexpected "1XX" responses are silently ignored as per the
460           specification.
461
462       •   Only 'chunked' "Transfer-Encoding" is supported.
463
464       •   There is no support for a Request-URI of '*' for the 'OPTIONS'
465           request.
466
467       •   Headers mentioned in the RFCs and some other, well-known headers
468           are generated with their canonical case.  Other headers are sent in
469           the case provided by the user.  Except for control headers (which
470           are sent first), headers are sent in arbitrary order.
471
472       Despite the limitations listed above, HTTP::Tiny is considered feature-
473       complete.  New feature requests should be directed to HTTP::Tiny::UA.
474

SEE ALSO

476       •   HTTP::Tiny::UA - Higher level UA features for HTTP::Tiny
477
478       •   HTTP::Thin - HTTP::Tiny wrapper with HTTP::Request/HTTP::Response
479           compatibility
480
481       •   HTTP::Tiny::Mech - Wrap WWW::Mechanize instance in HTTP::Tiny
482           compatible interface
483
484       •   IO::Socket::IP - Required for IPv6 support
485
486       •   IO::Socket::SSL - Required for SSL support
487
488       •   LWP::UserAgent - If HTTP::Tiny isn't enough for you, this is the
489           "standard" way to do things
490
491       •   Mozilla::CA - Required if you want to validate SSL certificates
492
493       •   Net::SSLeay - Required for SSL support
494

SUPPORT

496   Bugs / Feature Requests
497       Please report any bugs or feature requests through the issue tracker at
498       <https://github.com/chansen/p5-http-tiny/issues>.  You will be notified
499       automatically of any progress on your issue.
500
501   Source Code
502       This is open source software.  The code repository is available for
503       public review and contribution under the terms of the license.
504
505       <https://github.com/chansen/p5-http-tiny>
506
507         git clone https://github.com/chansen/p5-http-tiny.git
508

AUTHORS

510       •   Christian Hansen <chansen@cpan.org>
511
512       •   David Golden <dagolden@cpan.org>
513

CONTRIBUTORS

515       •   Alan Gardner <gardner@pythian.com>
516
517       •   Alessandro Ghedini <al3xbio@gmail.com>
518
519       •   A. Sinan Unur <nanis@cpan.org>
520
521       •   Brad Gilbert <bgills@cpan.org>
522
523       •   brian m. carlson <sandals@crustytoothpaste.net>
524
525       •   Chris Nehren <apeiron@cpan.org>
526
527       •   Chris Weyl <cweyl@alumni.drew.edu>
528
529       •   Claes Jakobsson <claes@surfar.nu>
530
531       •   Clinton Gormley <clint@traveljury.com>
532
533       •   Craig A. Berry <craigberry@mac.com>
534
535       •   Craig Berry <cberry@cpan.org>
536
537       •   David Golden <xdg@xdg.me>
538
539       •   David Mitchell <davem@iabyn.com>
540
541       •   Dean Pearce <pearce@pythian.com>
542
543       •   Edward Zborowski <ed@rubensteintech.com>
544
545       •   Felipe Gasper <felipe@felipegasper.com>
546
547       •   Greg Kennedy <kennedy.greg@gmail.com>
548
549       •   James E Keenan <jkeenan@cpan.org>
550
551       •   James Raspass <jraspass@gmail.com>
552
553       •   Jeremy Mates <jmates@cpan.org>
554
555       •   Jess Robinson <castaway@desert-island.me.uk>
556
557       •   Karen Etheridge <ether@cpan.org>
558
559       •   Lukas Eklund <leklund@gmail.com>
560
561       •   Martin J. Evans <mjegh@ntlworld.com>
562
563       •   Martin-Louis Bright <mlbright@gmail.com>
564
565       •   Matthew Horsfall <wolfsage@gmail.com>
566
567       •   Michael R. Davis <mrdvt92@users.noreply.github.com>
568
569       •   Mike Doherty <doherty@cpan.org>
570
571       •   Nicolas Rochelemagne <rochelemagne@cpanel.net>
572
573       •   Olaf Alders <olaf@wundersolutions.com>
574
575       •   Olivier Mengué <dolmen@cpan.org>
576
577       •   Petr Písař <ppisar@redhat.com>
578
579       •   sanjay-cpu <snjkmr32@gmail.com>
580
581       •   Serguei Trouchelle <stro@cpan.org>
582
583       •   Shoichi Kaji <skaji@cpan.org>
584
585       •   SkyMarshal <skymarshal1729@gmail.com>
586
587       •   Sören Kornetzki <soeren.kornetzki@delti.com>
588
589       •   Steve Grazzini <steve.grazzini@grantstreet.com>
590
591       •   Syohei YOSHIDA <syohex@gmail.com>
592
593       •   Tatsuhiko Miyagawa <miyagawa@bulknews.net>
594
595       •   Tom Hukins <tom@eborcom.com>
596
597       •   Tony Cook <tony@develop-help.com>
598
599       •   Xavier Guimard <yadd@debian.org>
600
602       This software is copyright (c) 2021 by Christian Hansen.
603
604       This is free software; you can redistribute it and/or modify it under
605       the same terms as the Perl 5 programming language system itself.
606
607
608
609perl v5.34.0                      2021-11-08                     HTTP::Tiny(3)
Impressum