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