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

NAME

6       Net::HTTP - Low-level HTTP connection (client)
7

VERSION

9       version 6.23
10

SYNOPSIS

12        use Net::HTTP;
13        my $s = Net::HTTP->new(Host => "www.perl.com") || die $@;
14        $s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0");
15        my($code, $mess, %h) = $s->read_response_headers;
16
17        while (1) {
18           my $buf;
19           my $n = $s->read_entity_body($buf, 1024);
20           die "read failed: $!" unless defined $n;
21           last unless $n;
22           print $buf;
23        }
24

DESCRIPTION

26       The "Net::HTTP" class is a low-level HTTP client.  An instance of the
27       "Net::HTTP" class represents a connection to an HTTP server.  The HTTP
28       protocol is described in RFC 2616.  The "Net::HTTP" class supports
29       "HTTP/1.0" and "HTTP/1.1".
30
31       "Net::HTTP" is a sub-class of one of "IO::Socket::IP" (IPv6+IPv4),
32       "IO::Socket::INET6" (IPv6+IPv4), or "IO::Socket::INET" (IPv4 only).
33       You can mix the methods described below with reading and writing from
34       the socket directly.  This is not necessary a good idea, unless you
35       know what you are doing.
36
37       The following methods are provided (in addition to those of
38       "IO::Socket::INET"):
39
40       "$s = Net::HTTP->new( %options )"
41           The "Net::HTTP" constructor method takes the same options as
42           "IO::Socket::INET"'s as well as these:
43
44             Host:            Initial host attribute value
45             KeepAlive:       Initial keep_alive attribute value
46             SendTE:          Initial send_te attribute_value
47             HTTPVersion:     Initial http_version attribute value
48             PeerHTTPVersion: Initial peer_http_version attribute value
49             MaxLineLength:   Initial max_line_length attribute value
50             MaxHeaderLines:  Initial max_header_lines attribute value
51
52           The "Host" option is also the default for "IO::Socket::INET"'s
53           "PeerAddr".  The "PeerPort" defaults to 80 if not provided.  The
54           "PeerPort" specification can also be embedded in the "PeerAddr" by
55           preceding it with a ":", and closing the IPv6 address on brackets
56           "[]" if necessary:
57           "192.0.2.1:80","[2001:db8::1]:80","any.example.com:80".
58
59           The "Listen" option provided by "IO::Socket::INET"'s constructor
60           method is not allowed.
61
62           If unable to connect to the given HTTP server then the constructor
63           returns "undef" and $@ contains the reason.  After a successful
64           connect, a "Net:HTTP" object is returned.
65
66       "$s->host"
67           Get/set the default value of the "Host" header to send.  The $host
68           must not be set to an empty string (or "undef") for HTTP/1.1.
69
70       "$s->keep_alive"
71           Get/set the keep-alive value.  If this value is TRUE then the
72           request will be sent with headers indicating that the server should
73           try to keep the connection open so that multiple requests can be
74           sent.
75
76           The actual headers set will depend on the value of the
77           "http_version" and "peer_http_version" attributes.
78
79       "$s->send_te"
80           Get/set the a value indicating if the request will be sent with a
81           "TE" header to indicate the transfer encodings that the server can
82           choose to use.  The list of encodings announced as accepted by this
83           client depends on availability of the following modules:
84           "Compress::Raw::Zlib" for deflate, and "IO::Compress::Gunzip" for
85           gzip.
86
87       "$s->http_version"
88           Get/set the HTTP version number that this client should announce.
89           This value can only be set to "1.0" or "1.1".  The default is
90           "1.1".
91
92       "$s->peer_http_version"
93           Get/set the protocol version number of our peer.  This value will
94           initially be "1.0", but will be updated by a successful
95           read_response_headers() method call.
96
97       "$s->max_line_length"
98           Get/set a limit on the length of response line and response header
99           lines.  The default is 8192.  A value of 0 means no limit.
100
101       "$s->max_header_length"
102           Get/set a limit on the number of header lines that a response can
103           have.  The default is 128.  A value of 0 means no limit.
104
105       "$s->format_request($method, $uri, %headers, [$content])"
106           Format a request message and return it as a string.  If the headers
107           do not include a "Host" header, then a header is inserted with the
108           value of the "host" attribute.  Headers like "Connection" and
109           "Keep-Alive" might also be added depending on the status of the
110           "keep_alive" attribute.
111
112           If $content is given (and it is non-empty), then a "Content-Length"
113           header is automatically added unless it was already present.
114
115       "$s->write_request($method, $uri, %headers, [$content])"
116           Format and send a request message.  Arguments are the same as for
117           format_request().  Returns true if successful.
118
119       "$s->format_chunk( $data )"
120           Returns the string to be written for the given chunk of data.
121
122       "$s->write_chunk($data)"
123           Will write a new chunk of request entity body data.  This method
124           should only be used if the "Transfer-Encoding" header with a value
125           of "chunked" was sent in the request.  Note, writing zero-length
126           data is a no-op.  Use the write_chunk_eof() method to signal end of
127           entity body data.
128
129           Returns true if successful.
130
131       "$s->format_chunk_eof( %trailers )"
132           Returns the string to be written for signaling EOF when a
133           "Transfer-Encoding" of "chunked" is used.
134
135       "$s->write_chunk_eof( %trailers )"
136           Will write eof marker for chunked data and optional trailers.  Note
137           that trailers should not really be used unless is was signaled with
138           a "Trailer" header.
139
140           Returns true if successful.
141
142       "($code, $mess, %headers) = $s->read_response_headers( %opts )"
143           Read response headers from server and return it.  The $code is the
144           3 digit HTTP status code (see HTTP::Status) and $mess is the
145           textual message that came with it.  Headers are then returned as
146           key/value pairs.  Since key letter casing is not normalized and the
147           same key can even occur multiple times, assigning these values
148           directly to a hash is not wise.  Only the $code is returned if this
149           method is called in scalar context.
150
151           As a side effect this method updates the 'peer_http_version'
152           attribute.
153
154           Options might be passed in as key/value pairs.  There are currently
155           only two options supported; "laxed" and "junk_out".
156
157           The "laxed" option will make read_response_headers() more forgiving
158           towards servers that have not learned how to speak HTTP properly.
159           The "laxed" option is a boolean flag, and is enabled by passing in
160           a TRUE value.  The "junk_out" option can be used to capture bad
161           header lines when "laxed" is enabled.  The value should be an array
162           reference.  Bad header lines will be pushed onto the array.
163
164           The "laxed" option must be specified in order to communicate with
165           pre-HTTP/1.0 servers that don't describe the response outcome or
166           the data they send back with a header block.  For these servers
167           peer_http_version is set to "0.9" and this method returns (200,
168           "Assumed OK").
169
170           The method will raise an exception (die) if the server does not
171           speak proper HTTP or if the "max_line_length" or
172           "max_header_length" limits are reached.  If the "laxed" option is
173           turned on and "max_line_length" and "max_header_length" checks are
174           turned off, then no exception will be raised and this method will
175           always return a response code.
176
177       "$n = $s->read_entity_body($buf, $size);"
178           Reads chunks of the entity body content.  Basically the same
179           interface as for read() and sysread(), but the buffer offset
180           argument is not supported yet.  This method should only be called
181           after a successful read_response_headers() call.
182
183           The return value will be "undef" on read errors, 0 on EOF, -1 if no
184           data could be returned this time, otherwise the number of bytes
185           assigned to $buf.  The $buf is set to "" when the return value is
186           -1.
187
188           You normally want to retry this call if this function returns
189           either -1 or "undef" with $! as EINTR or EAGAIN (see Errno).  EINTR
190           can happen if the application catches signals and EAGAIN can happen
191           if you made the socket non-blocking.
192
193           This method will raise exceptions (die) if the server does not
194           speak proper HTTP.  This can only happen when reading chunked data.
195
196       "%headers = $s->get_trailers"
197           After read_entity_body() has returned 0 to indicate end of the
198           entity body, you might call this method to pick up any trailers.
199
200       "$s->_rbuf"
201           Get/set the read buffer content.  The read_response_headers() and
202           read_entity_body() methods use an internal buffer which they will
203           look for data before they actually sysread more from the socket
204           itself.  If they read too much, the remaining data will be left in
205           this buffer.
206
207       "$s->_rbuf_length"
208           Returns the number of bytes in the read buffer.  This should always
209           be the same as:
210
211               length($s->_rbuf)
212
213           but might be more efficient.
214

SUBCLASSING

216       The read_response_headers() and read_entity_body() will invoke the
217       sysread() method when they need more data.  Subclasses might want to
218       override this method to control how reading takes place.
219
220       The object itself is a glob.  Subclasses should avoid using hash key
221       names prefixed with "http_" and "io_".
222

SEE ALSO

224       LWP, IO::Socket::INET, Net::HTTP::NB
225

AUTHOR

227       Gisle Aas <gisle@activestate.com>
228
230       This software is copyright (c) 2001 by Gisle Aas.
231
232       This is free software; you can redistribute it and/or modify it under
233       the same terms as the Perl 5 programming language system itself.
234
235
236
237perl v5.38.0                      2023-07-21                      Net::HTTP(3)
Impressum