1HTTP::Tiny(3) User Contributed Perl Documentation HTTP::Tiny(3)
2
3
4
6 HTTP::Tiny - A small, simple, correct HTTP/1.1 client
7
9 version 0.076
10
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
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
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, responses larger than this will return
68 an exception.
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, an exception is
85 thrown.
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 Passing an explicit "undef" for "proxy", "http_proxy" or "https_proxy"
94 will prevent getting the corresponding proxies from the environment.
95
96 Exceptions from "max_size", "timeout" or other errors will result in a
97 pseudo-HTTP status code of 599 and a reason of "Internal Exception".
98 The content field in the response will contain the text of the
99 exception.
100
101 The "keep_alive" parameter enables a persistent connection, but only to
102 a single destination scheme, host and port. Also, if any connection-
103 relevant attributes are modified, or if the process ID or thread ID
104 change, the persistent connection will be dropped. If you want
105 persistent connections across multiple destinations, use multiple
106 HTTP::Tiny objects.
107
108 See "SSL SUPPORT" for more on the "verify_SSL" and "SSL_options"
109 attributes.
110
111 get|head|put|post|delete
112 $response = $http->get($url);
113 $response = $http->get($url, \%options);
114 $response = $http->head($url);
115
116 These methods are shorthand for calling "request()" for the given
117 method. The URL must have unsafe characters escaped and international
118 domain names encoded. See "request()" for valid options and a
119 description of the response.
120
121 The "success" field of the response will be true if the status code is
122 2XX.
123
124 post_form
125 $response = $http->post_form($url, $form_data);
126 $response = $http->post_form($url, $form_data, \%options);
127
128 This method executes a "POST" request and sends the key/value pairs
129 from a form data hash or array reference to the given URL with a
130 "content-type" of "application/x-www-form-urlencoded". If data is
131 provided as an array reference, the order is preserved; if provided as
132 a hash reference, the terms are sorted on key and value for
133 consistency. See documentation for the "www_form_urlencode" method for
134 details on the encoding.
135
136 The URL must have unsafe characters escaped and international domain
137 names encoded. See "request()" for valid options and a description of
138 the response. Any "content-type" header or content in the options
139 hashref will be ignored.
140
141 The "success" field of the response will be true if the status code is
142 2XX.
143
144 mirror
145 $response = $http->mirror($url, $file, \%options)
146 if ( $response->{success} ) {
147 print "$file is up to date\n";
148 }
149
150 Executes a "GET" request for the URL and saves the response body to the
151 file name provided. The URL must have unsafe characters escaped and
152 international domain names encoded. If the file already exists, the
153 request will include an "If-Modified-Since" header with the
154 modification timestamp of the file. You may specify a different
155 "If-Modified-Since" header yourself in the "$options->{headers}" hash.
156
157 The "success" field of the response will be true if the status code is
158 2XX or if the status code is 304 (unmodified).
159
160 If the file was modified and the server response includes a properly
161 formatted "Last-Modified" header, the file modification time will be
162 updated accordingly.
163
164 request
165 $response = $http->request($method, $url);
166 $response = $http->request($method, $url, \%options);
167
168 Executes an HTTP request of the given method type ('GET', 'HEAD',
169 'POST', 'PUT', etc.) on the given URL. The URL must have unsafe
170 characters escaped and international domain names encoded.
171
172 NOTE: Method names are case-sensitive per the HTTP/1.1 specification.
173 Don't use "get" when you really want "GET". See LIMITATIONS for how
174 this applies to redirection.
175
176 If the URL includes a "user:password" stanza, they will be used for
177 Basic-style authorization headers. (Authorization headers will not be
178 included in a redirected request.) For example:
179
180 $http->request('GET', 'http://Aladdin:open sesame@example.com/');
181
182 If the "user:password" stanza contains reserved characters, they must
183 be percent-escaped:
184
185 $http->request('GET', 'http://john%40example.com:password@example.com/');
186
187 A hashref of options may be appended to modify the request.
188
189 Valid options are:
190
191 · "headers" — A hashref containing headers to include with the
192 request. If the value for a header is an array reference, the
193 header will be output multiple times with each value in the array.
194 These headers over-write any default headers.
195
196 · "content" — A scalar to include as the body of the request OR a
197 code reference that will be called iteratively to produce the body
198 of the request
199
200 · "trailer_callback" — A code reference that will be called if it
201 exists to provide a hashref of trailing headers (only used with
202 chunked transfer-encoding)
203
204 · "data_callback" — A code reference that will be called for each
205 chunks of the response body received.
206
207 · "peer" — Override host resolution and force all connections to go
208 only to a specific peer address, regardless of the URL of the
209 request. This will include any redirections! This options should
210 be used with extreme caution (e.g. debugging or very special
211 circumstances). It can be given as either a scalar or a code
212 reference that will receive the hostname and whose response will be
213 taken as the address.
214
215 The "Host" header is generated from the URL in accordance with RFC
216 2616. It is a fatal error to specify "Host" in the "headers" option.
217 Other headers may be ignored or overwritten if necessary for transport
218 compliance.
219
220 If the "content" option is a code reference, it will be called
221 iteratively to provide the content body of the request. It should
222 return the empty string or undef when the iterator is exhausted.
223
224 If the "content" option is the empty string, no "content-type" or
225 "content-length" headers will be generated.
226
227 If the "data_callback" option is provided, it will be called
228 iteratively until the entire response body is received. The first
229 argument will be a string containing a chunk of the response body, the
230 second argument will be the in-progress response hash reference, as
231 described below. (This allows customizing the action of the callback
232 based on the "status" or "headers" received prior to the content body.)
233
234 The "request" method returns a hashref containing the response. The
235 hashref will have the following keys:
236
237 · "success" — Boolean indicating whether the operation returned a 2XX
238 status code
239
240 · "url" — URL that provided the response. This is the URL of the
241 request unless there were redirections, in which case it is the
242 last URL queried in a redirection chain
243
244 · "status" — The HTTP status code of the response
245
246 · "reason" — The response phrase returned by the server
247
248 · "content" — The body of the response. If the response does not
249 have any content or if a data callback is provided to consume the
250 response body, this will be the empty string
251
252 · "headers" — A hashref of header fields. All header field names
253 will be normalized to be lower case. If a header is repeated, the
254 value will be an arrayref; it will otherwise be a scalar string
255 containing the value
256
257 · "protocol" - If this field exists, it is the protocol of the
258 response such as HTTP/1.0 or HTTP/1.1
259
260 · "redirects" If this field exists, it is an arrayref of response
261 hash references from redirects in the same order that redirections
262 occurred. If it does not exist, then no redirections occurred.
263
264 On an exception during the execution of the request, the "status" field
265 will contain 599, and the "content" field will contain the text of the
266 exception.
267
268 www_form_urlencode
269 $params = $http->www_form_urlencode( $data );
270 $response = $http->get("http://example.com/query?$params");
271
272 This method converts the key/value pairs from a data hash or array
273 reference into a "x-www-form-urlencoded" string. The keys and values
274 from the data reference will be UTF-8 encoded and escaped per RFC 3986.
275 If a value is an array reference, the key will be repeated with each of
276 the values of the array reference. If data is provided as a hash
277 reference, the key/value pairs in the resulting string will be sorted
278 by key and value for consistent ordering.
279
280 can_ssl
281 $ok = HTTP::Tiny->can_ssl;
282 ($ok, $why) = HTTP::Tiny->can_ssl;
283 ($ok, $why) = $http->can_ssl;
284
285 Indicates if SSL support is available. When called as a class object,
286 it checks for the correct version of Net::SSLeay and IO::Socket::SSL.
287 When called as an object methods, if "SSL_verify" is true or if
288 "SSL_verify_mode" is set in "SSL_options", it checks that a CA file is
289 available.
290
291 In scalar context, returns a boolean indicating if SSL is available.
292 In list context, returns the boolean and a (possibly multi-line) string
293 of errors indicating why SSL isn't available.
294
295 connected
296 $host = $http->connected;
297 ($host, $port) = $http->connected;
298
299 Indicates if a connection to a peer is being kept alive, per the
300 "keep_alive" option.
301
302 In scalar context, returns the peer host and port, joined with a colon,
303 or "undef" (if no peer is connected). In list context, returns the
304 peer host and port or an empty list (if no peer is connected).
305
306 Note: This method cannot reliably be used to discover whether the
307 remote host has closed its end of the socket.
308
310 Direct "https" connections are supported only if IO::Socket::SSL 1.56
311 or greater and Net::SSLeay 1.49 or greater are installed. An exception
312 will be thrown if new enough versions of these modules are not
313 installed or if the SSL encryption fails. You can also use
314 "HTTP::Tiny::can_ssl()" utility function that returns boolean to see if
315 the required modules are installed.
316
317 An "https" connection may be made via an "http" proxy that supports the
318 CONNECT command (i.e. RFC 2817). You may not proxy "https" via a proxy
319 that itself requires "https" to communicate.
320
321 SSL provides two distinct capabilities:
322
323 · Encrypted communication channel
324
325 · Verification of server identity
326
327 By default, HTTP::Tiny does not verify server identity.
328
329 Server identity verification is controversial and potentially tricky
330 because it depends on a (usually paid) third-party Certificate
331 Authority (CA) trust model to validate a certificate as legitimate.
332 This discriminates against servers with self-signed certificates or
333 certificates signed by free, community-driven CA's such as CAcert.org
334 <http://cacert.org>.
335
336 By default, HTTP::Tiny does not make any assumptions about your trust
337 model, threat level or risk tolerance. It just aims to give you an
338 encrypted channel when you need one.
339
340 Setting the "verify_SSL" attribute to a true value will make HTTP::Tiny
341 verify that an SSL connection has a valid SSL certificate corresponding
342 to the host name of the connection and that the SSL certificate has
343 been verified by a CA. Assuming you trust the CA, this will protect
344 against a man-in-the-middle attack <http://en.wikipedia.org/wiki/Man-
345 in-the-middle_attack>. If you are concerned about security, you should
346 enable this option.
347
348 Certificate verification requires a file containing trusted CA
349 certificates.
350
351 If the environment variable "SSL_CERT_FILE" is present, HTTP::Tiny will
352 try to find a CA certificate file in that location.
353
354 If the Mozilla::CA module is installed, HTTP::Tiny will use the CA file
355 included with it as a source of trusted CA's. (This means you trust
356 Mozilla, the author of Mozilla::CA, the CPAN mirror where you got
357 Mozilla::CA, the toolchain used to install it, and your operating
358 system security, right?)
359
360 If that module is not available, then HTTP::Tiny will search several
361 system-specific default locations for a CA certificate file:
362
363 · /etc/ssl/certs/ca-certificates.crt
364
365 · /etc/pki/tls/certs/ca-bundle.crt
366
367 · /etc/ssl/ca-bundle.pem
368
369 An exception will be raised if "verify_SSL" is true and no CA
370 certificate file is available.
371
372 If you desire complete control over SSL connections, the "SSL_options"
373 attribute lets you provide a hash reference that will be passed through
374 to "IO::Socket::SSL::start_SSL()", overriding any options set by
375 HTTP::Tiny. For example, to provide your own trusted CA file:
376
377 SSL_options => {
378 SSL_ca_file => $file_path,
379 }
380
381 The "SSL_options" attribute could also be used for such things as
382 providing a client certificate for authentication to a server or
383 controlling the choice of cipher used for the SSL connection. See
384 IO::Socket::SSL documentation for details.
385
387 HTTP::Tiny can proxy both "http" and "https" requests. Only Basic
388 proxy authorization is supported and it must be provided as part of the
389 proxy URL: "http://user:pass@proxy.example.com/".
390
391 HTTP::Tiny supports the following proxy environment variables:
392
393 · http_proxy or HTTP_PROXY
394
395 · https_proxy or HTTPS_PROXY
396
397 · all_proxy or ALL_PROXY
398
399 If the "REQUEST_METHOD" environment variable is set, then this might be
400 a CGI process and "HTTP_PROXY" would be set from the "Proxy:" header,
401 which is a security risk. If "REQUEST_METHOD" is set, "HTTP_PROXY"
402 (the upper case variant only) is ignored.
403
404 Tunnelling "https" over an "http" proxy using the CONNECT method is
405 supported. If your proxy uses "https" itself, you can not tunnel
406 "https" over it.
407
408 Be warned that proxying an "https" connection opens you to the risk of
409 a man-in-the-middle attack by the proxy server.
410
411 The "no_proxy" environment variable is supported in the format of a
412 comma-separated list of domain extensions proxy should not be used for.
413
414 Proxy arguments passed to "new" will override their corresponding
415 environment variables.
416
418 HTTP::Tiny is conditionally compliant with the HTTP/1.1 specifications
419 <http://www.w3.org/Protocols/>:
420
421 · "Message Syntax and Routing" [RFC7230]
422
423 · "Semantics and Content" [RFC7231]
424
425 · "Conditional Requests" [RFC7232]
426
427 · "Range Requests" [RFC7233]
428
429 · "Caching" [RFC7234]
430
431 · "Authentication" [RFC7235]
432
433 It attempts to meet all "MUST" requirements of the specification, but
434 does not implement all "SHOULD" requirements. (Note: it was developed
435 against the earlier RFC 2616 specification and may not yet meet the
436 revised RFC 7230-7235 spec.)
437
438 Some particular limitations of note include:
439
440 · HTTP::Tiny focuses on correct transport. Users are responsible for
441 ensuring that user-defined headers and content are compliant with
442 the HTTP/1.1 specification.
443
444 · Users must ensure that URLs are properly escaped for unsafe
445 characters and that international domain names are properly encoded
446 to ASCII. See URI::Escape, URI::_punycode and Net::IDN::Encode.
447
448 · Redirection is very strict against the specification. Redirection
449 is only automatic for response codes 301, 302, 307 and 308 if the
450 request method is 'GET' or 'HEAD'. Response code 303 is always
451 converted into a 'GET' redirection, as mandated by the
452 specification. There is no automatic support for status 305 ("Use
453 proxy") redirections.
454
455 · There is no provision for delaying a request body using an "Expect"
456 header. Unexpected "1XX" responses are silently ignored as per the
457 specification.
458
459 · Only 'chunked' "Transfer-Encoding" is supported.
460
461 · There is no support for a Request-URI of '*' for the 'OPTIONS'
462 request.
463
464 · Headers mentioned in the RFCs and some other, well-known headers
465 are generated with their canonical case. Other headers are sent in
466 the case provided by the user. Except for control headers (which
467 are sent first), headers are sent in arbitrary order.
468
469 Despite the limitations listed above, HTTP::Tiny is considered feature-
470 complete. New feature requests should be directed to HTTP::Tiny::UA.
471
473 · HTTP::Tiny::UA - Higher level UA features for HTTP::Tiny
474
475 · HTTP::Thin - HTTP::Tiny wrapper with HTTP::Request/HTTP::Response
476 compatibility
477
478 · HTTP::Tiny::Mech - Wrap WWW::Mechanize instance in HTTP::Tiny
479 compatible interface
480
481 · IO::Socket::IP - Required for IPv6 support
482
483 · IO::Socket::SSL - Required for SSL support
484
485 · LWP::UserAgent - If HTTP::Tiny isn't enough for you, this is the
486 "standard" way to do things
487
488 · Mozilla::CA - Required if you want to validate SSL certificates
489
490 · Net::SSLeay - Required for SSL support
491
493 Bugs / Feature Requests
494 Please report any bugs or feature requests through the issue tracker at
495 <https://github.com/chansen/p5-http-tiny/issues>. You will be notified
496 automatically of any progress on your issue.
497
498 Source Code
499 This is open source software. The code repository is available for
500 public review and contribution under the terms of the license.
501
502 <https://github.com/chansen/p5-http-tiny>
503
504 git clone https://github.com/chansen/p5-http-tiny.git
505
507 · Christian Hansen <chansen@cpan.org>
508
509 · David Golden <dagolden@cpan.org>
510
512 · Alan Gardner <gardner@pythian.com>
513
514 · Alessandro Ghedini <al3xbio@gmail.com>
515
516 · A. Sinan Unur <nanis@cpan.org>
517
518 · Brad Gilbert <bgills@cpan.org>
519
520 · brian m. carlson <sandals@crustytoothpaste.net>
521
522 · Chris Nehren <apeiron@cpan.org>
523
524 · Chris Weyl <cweyl@alumni.drew.edu>
525
526 · Claes Jakobsson <claes@surfar.nu>
527
528 · Clinton Gormley <clint@traveljury.com>
529
530 · Craig A. Berry <craigberry@mac.com>
531
532 · Craig Berry <cberry@cpan.org>
533
534 · David Golden <xdg@xdg.me>
535
536 · David Mitchell <davem@iabyn.com>
537
538 · Dean Pearce <pearce@pythian.com>
539
540 · Edward Zborowski <ed@rubensteintech.com>
541
542 · Felipe Gasper <felipe@felipegasper.com>
543
544 · James Raspass <jraspass@gmail.com>
545
546 · Jeremy Mates <jmates@cpan.org>
547
548 · Jess Robinson <castaway@desert-island.me.uk>
549
550 · Karen Etheridge <ether@cpan.org>
551
552 · Lukas Eklund <leklund@gmail.com>
553
554 · Martin J. Evans <mjegh@ntlworld.com>
555
556 · Martin-Louis Bright <mlbright@gmail.com>
557
558 · Mike Doherty <doherty@cpan.org>
559
560 · Nicolas Rochelemagne <rochelemagne@cpanel.net>
561
562 · Olaf Alders <olaf@wundersolutions.com>
563
564 · Olivier Mengué <dolmen@cpan.org>
565
566 · Petr Písař <ppisar@redhat.com>
567
568 · Serguei Trouchelle <stro@cpan.org>
569
570 · Shoichi Kaji <skaji@cpan.org>
571
572 · SkyMarshal <skymarshal1729@gmail.com>
573
574 · Sören Kornetzki <soeren.kornetzki@delti.com>
575
576 · Steve Grazzini <steve.grazzini@grantstreet.com>
577
578 · Syohei YOSHIDA <syohex@gmail.com>
579
580 · Tatsuhiko Miyagawa <miyagawa@bulknews.net>
581
582 · Tom Hukins <tom@eborcom.com>
583
584 · Tony Cook <tony@develop-help.com>
585
587 This software is copyright (c) 2018 by Christian Hansen.
588
589 This is free software; you can redistribute it and/or modify it under
590 the same terms as the Perl 5 programming language system itself.
591
592
593
594perl v5.32.0 2020-07-28 HTTP::Tiny(3)