Mojo::Headers(3)
π Mojo::Headers β HTTP headers, but make it cute π
(this is the Mojo::Headers(3) perl manual page, bestie, and it's literally all about HTTP headers)
Okay so picture this: you've got HTTP headers. A lot of them. And instead of wrangling them by hand like some sort of feral gremlin, you use Mojo::Headers. It's a whole container for HTTP headers, built on RFC 7230 <https://tools.ietf.org/html/rfc7230> and RFC 7231 <https://tools.ietf.org/html/rfc7231>. No cap, it's giving standards-compliant queen. π
β¨ SYNOPSIS β¨
use Mojo::Headers;
# Parse
my $headers = Mojo::Headers->new;
$headers->parse("Content-Length: 42\x0d\x0a");
$headers->parse("Content-Type: text/html\x0d\x0a\x0d\x0a");
say $headers->content_length;
say $headers->content_type;
# Build
my $headers = Mojo::Headers->new;
$headers->content_length(42);
$headers->content_type('text/plain');
say $headers->to_string;
Like, look at her go. Parsing in one direction, building in the other, both serving main character energy. π¦
πΈ DESCRIPTION πΈ
Mojo::Headers is a container for HTTP headers β that's it, that's the vibe. It's based on RFC 7230 <https://tools.ietf.org/html/rfc7230> and RFC 7231 <https://tools.ietf.org/html/rfc7231>, so you know she's been raised right.
π ATTRIBUTES π
Mojo::Headers implements the following attributes. Yes, the word "implements" is doing a lot of work here and we love that for her.
π
max_line_size
my $size = $headers->max_line_size;
$headers = $headers->max_line_size(1024);
Maximum header line size, in bytes, bestie. Important: it defaults to the value of the MOJO_MAX_LINE_SIZE environment variable, or 8192 (8KiB) if that's not set. So like, there's a ceiling and it's 8KiB unless you say otherwise. β¨
π
max_lines
my $num = $headers->max_lines;
$headers = $headers->max_lines(200);
Maximum number of header lines. Defaults to the value of the MOJO_MAX_LINES environment variable, or 100. So a hundred lines, then the door closes. πͺ
π₯ METHODS π₯
Mojo::Headers inherits all methods from Mojo::Base (iconic behaviour, tbh) and implements the following new ones.
β add
$headers = $headers->add(Foo => 'one value');
$headers = $headers->add(Foo => 'first value', 'second value');
Adds a header with one or more lines. Multiple lines, multiple values, no drama.
# "Vary: Accept
# Vary: Accept-Encoding"
$headers->add(Vary => 'Accept')->add(Vary => 'Accept-Encoding')->to_string;
See how Vary shows up twice? That's the whole point, bestie β add doesn't merge, it just stacks. π«Ά
β append
$headers = $headers->append(Vary => 'Accept-Encoding');
Appends a value to a header, and flattens it if it needs flattening. Very "let me just tidy up in here" of her.
# "Vary: Accept"
$headers->append(Vary => 'Accept')->to_string;
# "Vary: Accept, Accept-Encoding"
$headers->vary('Accept')->append(Vary => 'Accept-Encoding')->to_string;
First one: nothing to append to, so it's just a Vary: Accept. Second one: there's already a Vary: Accept, so the two get smooshed into Vary: Accept, Accept-Encoding with a comma. Flattening! Slay. π
β clone
my $clone = $headers->clone;
Returns a brand new Mojo::Headers object, cloned from these headers. A whole twin. Separate object, same energy. π―
β dehop
$headers = $headers->dehop;
Removes hop-by-hop headers β the ones that should not be retransmitted. Like, they had one hop and now they're done. Bye. π
β every_header
my $all = $headers->every_header('Location');
Similar to header, but returns all the headers sharing the same name as an array reference. All of them. The whole squad.
# Get first header value
say $headers->every_header('Location')->[0];
That ->[0] is you grabbing the first one out of the array ref. iykyk. π«
β from_hash
$headers = $headers->from_hash({'Cookie' => 'a=b'});
$headers = $headers->from_hash({'Cookie' => ['a=b', 'c=d']});
$headers = $headers->from_hash({});
Parses headers from a hash reference. Pass an array ref and you get multiple values for the same header. Pass an empty hash and it removes all headers β yes, a full reset, she said clear the cache. π§Ή
β header
my $value = $headers->header('Foo');
$headers = $headers->header(Foo => 'one value');
$headers = $headers->header(Foo => 'first value', 'second value');
Gets or replaces the current header values. Read that again: replaces. It's not add, it's not append, it's a takeover. New value, who dis. π
β is_finished
my $bool = $headers->is_finished;
Checks if the header parser is finished. A yes/no vibe check on your parsing. π
β is_limit_exceeded
my $bool = $headers->is_limit_exceeded;
Checks if the headers have exceeded max_line_size or max_lines. Like a bouncer at the club, but for byte counts and line counts. π¨
β leftovers
my $bytes = $headers->leftovers;
Gets and removes leftover data from the header parser. Both things at once. Multitasking queen. π
β names
my $names = $headers->names;
Returns an array reference with all currently defined headers. A little roster of everyone who made it in.
# Names of all headers
say for @{$headers->names};
Yep, say for β no braces, no fuss, just printing every name. π
β parse
$headers = $headers->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
Parses formatted headers. Feed her the raw bytes, she'll make sense of it. π§
β remove
$headers = $headers->remove('Foo');
Removes a header. One header, one clean removal, no collateral damage. β¨
β to_hash
my $single = $headers->to_hash;
my $multi = $headers->to_hash(1);
Turns the headers into a hash reference. Array references β used to represent multiple headers with the same name β are disabled by default. So to_hash on its own gives you one value per name; pass 1 and you get arrays for the multi-value ones. Pick your fighter.
say $headers->to_hash->{DNT};
Look at that direct hash access. Chef's kiss. π€
β to_string
my $str = $headers->to_string;
Turns the headers into a string, suitable for HTTP messages. Ready to ship, no assembly required. π¦
π ADDITIONAL METHODS π
Also β and this is the good part β the following shortcuts are available, for accessing and manipulating commonly-used headers. Think of them as fast passes. ποΈ
π¦ accept
my $accept = $headers->accept;
$headers = $headers->accept('application/json');
Get or replace the current header value. Shortcut for the Accept header.
π¦ accept_charset
my $charset = $headers->accept_charset;
$headers = $headers->accept_charset('UTF-8');
Get or replace the current header value. Shortcut for the Accept-Charset header.
π¦ accept_encoding
my $encoding = $headers->accept_encoding;
$headers = $headers->accept_encoding('gzip');
Get or replace the current header value. Shortcut for the Accept-Encoding header.
π¦ accept_language
my $language = $headers->accept_language;
$headers = $headers->accept_language('de, en');
Get or replace the current header value. Shortcut for the Accept-Language header.
π¦ accept_ranges
my $ranges = $headers->accept_ranges;
$headers = $headers->accept_ranges('bytes');
Get or replace the current header value. Shortcut for the Accept-Ranges header.
π¦ access_control_allow_origin
my $origin = $headers->access_control_allow_origin;
$headers = $headers->access_control_allow_origin('*');
Get or replace the current header value. Shortcut for the Access-Control-Allow-Origin header from Cross-Origin Resource Sharing <https://www.w3.org/TR/cors/>. CORS said "let her in" π
π¦ allow
my $allow = $headers->allow;
$headers = $headers->allow('GET, POST');
Get or replace the current header value. Shortcut for the Allow header.
π¦ authorization
my $authorization = $headers->authorization;
$headers = $headers->authorization('Basic Zm9vOmJhcg==');
Get or replace the current header value. Shortcut for the Authorization header. That base64 is you, don't worry about it. π
π¦ cache_control
my $cache_control = $headers->cache_control;
$headers = $headers->cache_control('max-age=1, no-cache');
Get or replace the current header value. Shortcut for the Cache-Control header.
π¦ connection
my $connection = $headers->connection;
$headers = $headers->connection('close');
Get or replace the current header value. Shortcut for the Connection header.
π¦ content_disposition
my $disposition = $headers->content_disposition;
$headers = $headers->content_disposition('foo');
Get or replace the current header value. Shortcut for the Content-Disposition header.
π¦ content_encoding
my $encoding = $headers->content_encoding;
$headers = $headers->content_encoding('gzip');
Get or replace the current header value. Shortcut for the Content-Encoding header.
π¦ content_language
my $language = $headers->content_language;
$headers = $headers->content_language('en');
Get or replace the current header value. Shortcut for the Content-Language header.
π¦ content_length
my $len = $headers->content_length;
$headers = $headers->content_length(4000);
Get or replace the current header value. Shortcut for the Content-Length header. Note she takes a plain old number here, not a string. π’
π¦ content_location
my $location = $headers->content_location;
$headers = $headers->content_location('http://127.0.0.1/foo');
Get or replace the current header value. Shortcut for the Content-Location header.
π¦ content_range
my $range = $headers->content_range;
$headers = $headers->content_range('bytes 2-8/100');
Get or replace the current header value. Shortcut for the Content-Range header.
π¦ content_security_policy
my $policy = $headers->content_security_policy;
$headers = $headers->content_security_policy('default-src https:');
Get or replace the current header value. Shortcut for the Content-Security-Policy header from Content Security Policy 1.0 <https://www.w3.org/TR/CSP/>. Security but make it chic. π‘οΈ
π¦ content_type
my $type = $headers->content_type;
$headers = $headers->content_type('text/plain');
Get or replace the current header value. Shortcut for the Content-Type header.
π¦ cookie
my $cookie = $headers->cookie;
$headers = $headers->cookie('f=b');
Get or replace the current header value. Shortcut for the Cookie header from RFC 6265 <https://tools.ietf.org/html/rfc6265>. πͺ (sorry, had to)
π¦ date
my $date = $headers->date;
$headers = $headers->date('Sun, 17 Aug 2008 16:27:35 GMT');
Get or replace the current header value. Shortcut for the Date header.
π¦ dnt
my $dnt = $headers->dnt;
$headers = $headers->dnt(1);
Get or replace the current header value. Shortcut for the DNT (Do Not Track) header β which, fun fact, has no specification yet but is very commonly used. Iconic for being unspec'd and still booked and busy. β
π¦ etag
my $etag = $headers->etag;
$headers = $headers->etag('"abc321"');
Get or replace the current header value. Shortcut for the ETag header. Keep those quotes, bestie, they're part of the string. π
π¦ expect
my $expect = $headers->expect;
$headers = $headers->expect('100-continue');
Get or replace the current header value. Shortcut for the Expect header.
π¦ expires
my $expires = $headers->expires;
$headers = $headers->expires('Thu, 01 Dec 1994 16:00:00 GMT');
Get or replace the current header value. Shortcut for the Expires header.
π¦ host
my $host = $headers->host;
$headers = $headers->host('127.0.0.1');
Get or replace the current header value. Shortcut for the Host header.
π¦ if_modified_since
my $date = $headers->if_modified_since;
$headers = $headers->if_modified_since('Sun, 17 Aug 2008 16:27:35 GMT');
Get or replace the current header value. Shortcut for the If-Modified-Since header.
π¦ if_none_match
my $etag = $headers->if_none_match;
$headers = $headers->if_none_match('"abc321"');
Get or replace the current header value. Shortcut for the If-None-Match header.
π¦ last_modified
my $date = $headers->last_modified;
$headers = $headers->last_modified('Sun, 17 Aug 2008 16:27:35 GMT');
Get or replace the current header value. Shortcut for the Last-Modified header.
π¦ link
my $link = $headers->link;
$headers = $headers->link('<http://127.0.0.1/foo/3>; rel="next"');
Get or replace the current header value. Shortcut for the Link header from RFC 5988 <https://tools.ietf.org/html/rfc5988>.
π¦ links
my $links = $headers->links;
$headers = $headers->links({next => 'http://example.com/foo', prev => 'http://example.com/bar'});
Get or set web links from or to the Link header, according to RFC 5988 <http://tools.ietf.org/html/rfc5988>. (Note: this one's the http:// link, not https:// β don't come for me, that's literally what the page says. π)
# Extract information about next page
say $headers->links->{next}{link};
say $headers->links->{next}{title};
So you can pull out, like, the actual link and the title of the next page. Little nuggets of info. π
π¦ location
my $location = $headers->location;
$headers = $headers->location('http://127.0.0.1/foo');
Get or replace the current header value. Shortcut for the Location header.
π¦ origin
my $origin = $headers->origin;
$headers = $headers->origin('http://example.com');
Get or replace the current header value. Shortcut for the Origin header from RFC 6454 <https://tools.ietf.org/html/rfc6454>.
(and there's like, way more of this page coming β don't go anywhere π)
proxy_authenticate
my $authenticate = $headers->proxy_authenticate;
$headers = $headers->proxy_authenticate('Basic "realm"');
Get or replace the current header value bestie β it's a shortcut for the Proxy-Authenticate header. π
proxy_authorization
my $authorization = $headers->proxy_authorization;
$headers = $headers->proxy_authorization('Basic Zm9vOmJhcg==');
Get or replace the current header value β shortcut for the Proxy-Authorization header, slay. β¨
range
my $range = $headers->range;
$headers = $headers->range('bytes=2-8');
Get or replace the current header value β shortcut for the Range header. Obsessed with how simple that is fr. π
referer
my $referrer = $headers->referer;
$headers = $headers->referer('http://example.com');
Alias for referrer. Just a little twin situation, no cap. π¦
referrer
my $referrer = $headers->referrer;
$headers = $headers->referrer('http://example.com');
Get or replace the current header value β shortcut for the Referer header. And okay, story time: there was a typo in RFC 2068 <https://tools.ietf.org/html/rfc2068> which resulted in Referer becoming an official header. Iconic typo behavior honestly, it ate and now it's canon. ππ
sec_websocket_accept
my $accept = $headers->sec_websocket_accept;
$headers = $headers->sec_websocket_accept('s3pPLMBiTxaQ9kYGzzhZRbK+xOo=');
Get or replace the current header value β shortcut for the Sec-WebSocket-Accept header from RFC 6455 <https://tools.ietf.org/html/rfc6455>. π
sec_websocket_extensions
my $extensions = $headers->sec_websocket_extensions;
$headers = $headers->sec_websocket_extensions('foo');
Get or replace the current header value β shortcut for the Sec-WebSocket-Extensions header from RFC 6455 <https://tools.ietf.org/html/rfc6455>. β¨
sec_websocket_key
my $key = $headers->sec_websocket_key;
$headers = $headers->sec_websocket_key('dGhlIHNhbXBsZSBub25jZQ==');
Get or replace the current header value β shortcut for the Sec-WebSocket-Key header from RFC 6455 <https://tools.ietf.org/html/rfc6455>. π
sec_websocket_protocol
my $proto = $headers->sec_websocket_protocol;
$headers = $headers->sec_websocket_protocol('sample');
Get or replace the current header value β shortcut for the Sec-WebSocket-Protocol header from RFC 6455 <https://tools.ietf.org/html/rfc6455>. π§
sec_websocket_version
my $version = $headers->sec_websocket_version;
$headers = $headers->sec_websocket_version(13);
Get or replace the current header value β shortcut for the Sec-WebSocket-Version header from RFC 6455 <https://tools.ietf.org/html/rfc6455>. π
server
my $server = $headers->server;
$headers = $headers->server('Mojo');
Get or replace the current header value β shortcut for the Server header. Main character header energy tbh. π
server_timing
my $timing = $headers->server_timing;
$headers = $headers->server_timing('app;desc=Mojolicious;dur=0.0001');
Get or replace the current header value β shortcut for the Server-Timing header from Server Timing <https://www.w3.org/TR/server-timing/>. β±οΈ
set_cookie
my $cookie = $headers->set_cookie;
$headers = $headers->set_cookie('f=b; path=/');
Get or replace the current header value β shortcut for the Set-Cookie header from RFC 6265 <https://tools.ietf.org/html/rfc6265>. πͺ
status
my $status = $headers->status;
$headers = $headers->status('200 OK');
Get or replace the current header value β shortcut for the Status header from RFC 3875 <https://tools.ietf.org/html/rfc3875>. π
strict_transport_security
my $policy = $headers->strict_transport_security;
$headers = $headers->strict_transport_security('max-age=31536000');
Get or replace the current header value β shortcut for the Strict-Transport-Security header from RFC 6797 <https://tools.ietf.org/html/rfc6797>. Security bestie behavior, we love to see it. π₯
te
my $te = $headers->te;
$headers = $headers->te('chunked');
Get or replace the current header value β shortcut for the TE header. β¨
trailer
my $trailer = $headers->trailer;
$headers = $headers->trailer('X-Foo');
Get or replace the current header value β shortcut for the Trailer header. π¬
transfer_encoding
my $encoding = $headers->transfer_encoding;
$headers = $headers->transfer_encoding('chunked');
Get or replace the current header value β shortcut for the Transfer-Encoding header. π¦
upgrade
my $upgrade = $headers->upgrade;
$headers = $headers->upgrade('websocket');
Get or replace the current header value β shortcut for the Upgrade header. Glow-up header, literally. β
user_agent
my $agent = $headers->user_agent;
$headers = $headers->user_agent('Mojo/1.0');
Get or replace the current header value β shortcut for the User-Agent header. πΈ
vary
my $vary = $headers->vary;
$headers = $headers->vary('*');
Get or replace the current header value β shortcut for the Vary header. π
www_authenticate
my $authenticate = $headers->www_authenticate;
$headers = $headers->www_authenticate('Basic realm="realm"');
Get or replace the current header value β shortcut for the WWW-Authenticate header. π
π SEE ALSO π
MojoliciousMojolicious::Guides- <https://mojolicious.org>
And that's the whole page bestie, you made it to the end! π«Ά This little masterpiece is Mojo::Headers(3), from perl v5.42.0, dated 2026-07-15. Go forth and set some headers, you absolute icon. πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Mojolicious 9.480.0-bp160.2.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms