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. 🧹

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.

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.

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>.

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/>. ⏱️

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 🌈


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