Net::HTTP(3)
π Net::HTTP β the low-level HTTP client π
okay bestie, sit down, hydrate, because we're doing Net::HTTP, a page from the User Contributed Perl Documentation section (that's the 3 in Net::HTTP(3), iykyk β¨). It's a manual page and it's giving very "I talk to servers directly and I'm not sorry" energy. Let's get into it π
β¨ VERSION β¨
version 6.24 β that's the one, no cap πΈ
π¦ SYNOPSIS π¦
okay so here's the whole vibe in code form, copy it exactly bestie:
use Net::HTTP;
my $s = Net::HTTP->new(Host => "www.perl.com") || die $@;
$s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0");
my($code, $mess, %h) = $s->read_response_headers;
while (1) {
my $buf;
my $n = $s->read_entity_body($buf, 1024);
die "read failed: $!" unless defined $n;
last unless $n;
print $buf;
}
look at her. iconic. she connects, she asks, she reads, she loops. periodt π₯
π DESCRIPTION π
so the Net::HTTP class is a low-level HTTP client. One instance of Net::HTTP = one connection to an HTTP server. The HTTP protocol itself is described in RFC 2616, and this class supports HTTP/1.0 and HTTP/1.1 π«Ά
Also, plot twist: Net::HTTP is a sub-class of one of IO::Socket::IP (IPv6+IPv4), IO::Socket::INET6 (IPv6+IPv4), or IO::Socket::INET (IPv4 only). So you can mix the methods below with reading and writing from the socket directly. That said β and I need you to hear me β this is not necessary a good idea, unless you know what you are doing π (and no, bestie, "I read a blog post once" does not count as knowing what you are doing).
The following methods are provided (in addition to those of IO::Socket::INET):
$s = Net::HTTP->new( %options )β theNet::HTTPconstructor method. It takes the same options asIO::Socket::INET's, plus these:
Host: Initial host attribute value
KeepAlive: Initial keep_alive attribute value
SendTE: Initial send_te attribute_value
HTTPVersion: Initial http_version attribute value
PeerHTTPVersion: Initial peer_http_version attribute value
MaxLineLength: Initial max_line_length attribute value
MaxHeaderLines: Initial max_header_lines attribute value
The Host option is also the default for IO::Socket::INET's PeerAddr. The PeerPort defaults to 80 if you don't provide it. And the PeerPort can also be embedded in the PeerAddr by preceding it with a :, closing the IPv6 address on brackets [] if necessary: "192.0.2.1:80", "[2001:db8::1]:80", "any.example.com:80" π
The Listen option provided by IO::Socket::INET's constructor method is not allowed. Nope. Not the vibe. π
If it's unable to connect to the given HTTP server, then the constructor returns undef and $@ contains the reason. After a successful connect, a Net:HTTP object is returned π
$s->hostβ get/set the default value of theHostheader to send. The$hostmust not be set to an empty string (orundef) for HTTP/1.1.$s->keep_aliveβ get/set the keep-alive value. If this value is TRUE then the request will be sent with headers indicating that the server should try to keep the connection open so that multiple requests can be sent.
The actual headers set will depend on the value of the http_version and peer_http_version attributes.
$s->send_teβ get/set the value indicating if the request will be sent with aTEheader, to indicate the transfer encodings that the server can choose to use. The list of encodings announced as accepted by this client depends on availability of the following modules:Compress::Raw::Zlibfor deflate, andIO::Compress::Gunzipfor gzip π$s->http_versionβ get/set the HTTP version number that this client should announce. This value can only be set to"1.0"or"1.1". The default is"1.1".$s->peer_http_versionβ get/set the protocol version number of our peer. This value will initially be"1.0", but will be updated by a successfulread_response_headers()method call.$s->max_line_lengthβ get/set a limit on the length of response line and response header lines. The default is 8192. A value of 0 means no limit π¦$s->max_header_lengthβ get/set a limit on the number of header lines that a response can have. The default is 128. A value of 0 means no limit πΈ$s->format_request($method, $uri, %headers, [$content])β format a request message and return it as a string. If the headers do not include aHostheader, then a header is inserted with the value of thehostattribute. Headers likeConnectionandKeep-Alivemight also be added depending on the status of thekeep_aliveattribute.
If $content is given (and it is non-empty), then a Content-Length header is automatically added unless it was already present β¨
$s->write_request($method, $uri, %headers, [$content])β format and send a request message. Arguments are the same as forformat_request(). Returns true if successful.$s->format_chunk( $data )β returns the string to be written for the given chunk of data.$s->write_chunk($data)β will write a new chunk of request entity body data. This method should only be used if theTransfer-Encodingheader with a value ofchunkedwas sent in the request. Note, writing zero-length data is a no-op. Use thewrite_chunk_eof()method to signal end of entity body data.
Returns true if successful.
$s->format_chunk_eof( %trailers )β returns the string to be written for signaling EOF when aTransfer-Encodingofchunkedis used.$s->write_chunk_eof( %trailers )β will write eof marker for chunked data and optional trailers. Note that trailers should not really be used unless is was signaled with aTrailerheader.
Returns true if successful.
($code, $mess, %headers) = $s->read_response_headers( %opts )β read response headers from server and return it. The$codeis the 3 digit HTTP status code (see HTTP::Status) and$messis the textual message that came with it. Headers are then returned as key/value pairs. Since key letter casing is not normalized and the same key can even occur multiple times, assigning these values directly to a hash is not wise π. Only the$codeis returned if this method is called in scalar context.
As a side effect this method updates the peer_http_version attribute.
Options might be passed in as key/value pairs. There are currently only two options supported: "laxed" and "junk_out".
The "laxed" option will make read_response_headers() more forgiving towards servers that have not learned how to speak HTTP properly π. The "laxed" option is a boolean flag, and is enabled by passing in a TRUE value. The "junk_out" option can be used to capture bad header lines when "laxed" is enabled. The value should be an array reference. Bad header lines will be pushed onto the array.
The "laxed" option must be specified in order to communicate with pre-HTTP/1.0 servers that don't describe the response outcome or the data they send back with a header block. For these servers peer_http_version is set to "0.9" and this method returns (200, "Assumed OK").
The method will raise an exception (die) if the server does not speak proper HTTP or if the max_line_length or max_header_length limits are reached. If the "laxed" option is turned on and max_line_length and max_header_length checks are turned off, then no exception will be raised and this method will always return a response code π
$n = $s->read_entity_body($buf, $size);β reads chunks of the entity body content. Basically the same interface as forread()andsysread(), but the buffer offset argument is not supported yet. This method should only be called after a successfulread_response_headers()call.
The return value will be undef on read errors, 0 on EOF, -1 if no data could be returned this time, otherwise the number of bytes assigned to $buf. The $buf is set to "" when the return value is -1.
You normally want to retry this call if this function returns either -1 or undef with $! as EINTR or EAGAIN (see Errno). EINTR can happen if the application catches signals and EAGAIN can happen if you made the socket non-blocking.
This method will raise exceptions (die) if the server does not speak proper HTTP. This can only happen when reading chunked data π
%headers = $s->get_trailersβ afterread_entity_body()has returned 0 to indicate end of the entity body, you might call this method to pick up any trailers.$s->_rbufβ get/set the read buffer content. Theread_response_headers()andread_entity_body()methods use an internal buffer which they will look for data before they actuallysysreadmore from the socket itself. If they read too much, the remaining data will be left in this buffer.$s->_rbuf_lengthβ returns the number of bytes in the read buffer. This should always be the same as:
length($s->_rbuf)
but might be more efficient π
π§ SUBCLASSING π§
The read_response_headers() and read_entity_body() will invoke the sysread() method when they need more data. Subclasses might want to override this method to control how reading takes place.
The object itself is a glob. Subclasses should avoid using hash key names prefixed with http_ and io_ β
π SEE ALSO π
LWP, IO::Socket::INET, Net::HTTP::NB
πΈ AUTHOR πΈ
Gisle Aas <gisle@activestate.com>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2001 by Gisle Aas.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself π«Ά
and that's the whole page bestie!! π this one came out of perl v5.42.0 and was dated 2025-09-01, so if you're reading it way later, hi, you're a time traveler, slay πβ¨ bye!! π
π¦ where this page comes from
- fedora 43: perl-Net-HTTP 6.24-1.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms