Mojo::Content(3)

🌸 NAME 🌸

Okay bestie, this is Mojo::Content(3) from the User Contributed Perl Documentation β€” the HTTP content base class. Periodt. πŸ’…

✨ SYNOPSIS ✨

package Mojo::Content::MyContent;
use Mojo::Base 'Mojo::Content';

sub body_contains  {...}
sub body_size      {...}
sub get_body_chunk {...}

πŸ’– DESCRIPTION πŸ’–

So Mojo::Content is an abstract base class for HTTP content containers, based on RFC 7230 (https://tools.ietf.org/html/rfc7230) and RFC 7231 (https://tools.ietf.org/html/rfc7231), like Mojo::Content::MultiPart and Mojo::Content::Single. It's giving foundational icon behavior, no cap. 🫢

πŸŽ€ EVENTS πŸŽ€

Mojo::Content inherits all events from Mojo::EventEmitter and can emit these new ones, bestie. πŸ‘‘

body

$content->on(body => sub ($content) {...});

This one fires once all the headers have been parsed and the body starts. Main character moment for the body era. ✨

$content->on(body => sub ($content) {
  $content->auto_upgrade(0) if $content->headers->header('X-No-MultiPart');
});

drain

$content->on(drain => sub ($content, $offset) {...});

Emitted once all the data has been written. Like, fully flushed, bestie. πŸ’§

$content->on(drain => sub ($content) {
  $content->write_chunk(time);
});

read

$content->on(read => sub ($content, $bytes) {...});

Emitted when a new chunk of content arrives. Incoming data? We love to see it. πŸ¦‹

$content->on(read => sub ($content, $bytes) {
  say "Streaming: $bytes";
});

sse

$content->on(sse => sub ($content, $event) {...});

Emitted when a new Server-Sent Event (SSE) connection has been established, and for each new event that arrives.

⚠️ Heads up, bestie: this event is EXPERIMENTAL and may change without warning! 😭
$content->on(sse => sub ($content, $event) {
  if ($event) { say "Type: $event->{type}, Data: $event->{data}" }
  else        { say "SSE connection established" }
});

πŸ¦‹ ATTRIBUTES πŸ¦‹

Mojo::Content implements these attributes. Let's get into it. πŸ’–

auto_decompress

my $bool = $content->auto_decompress;
$content = $content->auto_decompress($bool);

Automatically decompresses the content if is_compressed is true. Self-care for compressed vibes. 🧚

auto_relax

my $bool = $content->auto_relax;
$content = $content->auto_relax($bool);

Tries to detect when relaxed parsing is necessary. Lowkey detective energy. πŸ”

headers

my $headers = $content->headers;
$content    = $content->headers(Mojo::Headers->new);

These are the content headers, and they default to a Mojo::Headers object. Iconic header container behavior. πŸŽ€

max_buffer_size

my $size = $content->max_buffer_size;
$content = $content->max_buffer_size(1024);

Maximum size in bytes of the buffer for the content parser. Defaults to the value of the MOJO_MAX_BUFFER_SIZE environment variable or 262144 (256KiB). Big buffer energy, but make it bounded. πŸ’…

max_leftover_size

my $size = $content->max_leftover_size;
$content = $content->max_leftover_size(1024);

Maximum size in bytes of the buffer for pipelined HTTP requests. Defaults to the value of the MOJO_MAX_LEFTOVER_SIZE environment variable or 262144 (256KiB). Leftover data stays in its lane. ✨

relaxed

my $bool = $content->relaxed;
$content = $content->relaxed($bool);

Activates relaxed parsing for responses that are terminated with a connection close. Chill parsing era. 🌸

skip_body

my $bool = $content->skip_body;
$content = $content->skip_body($bool);

Skips body parsing and finishes after the headers. Not me skipping the body like it's a boring sequel. πŸ’€

πŸ’… METHODS πŸ’…

Mojo::Content inherits all methods from Mojo::EventEmitter and implements these new ones. Let's go, bestie. πŸš€

body_contains

my $bool = $content->body_contains('foo bar baz');

Checks if the content contains a specific string. It's meant to be overloaded in a subclass. Customize responsibly. 🫢

body_size

my $size = $content->body_size;

Content size in bytes. It's meant to be overloaded in a subclass. Size check, but make it subclass chic. πŸ’–

boundary

my $boundary = $content->boundary;

Extracts the multipart boundary from the Content-Type header. Boundary extraction is lowkey a whole vibe. πŸŽ€

charset

my $charset = $content->charset;

Extracts the charset from the Content-Type header. Character encoding said hello. 🌈

clone

my $clone = $content->clone;

Returns a new Mojo::Content object cloned from this content if possible; otherwise it returns undef. Clone era, but only when it works. πŸ™ˆ

generate_body_chunk

my $bytes = $content->generate_body_chunk(0);

Generates dynamic content. Dynamic content generator said periodt. ✨

get_body_chunk

my $bytes = $content->get_body_chunk(0);

Gets a chunk of content starting from a specific position. It's meant to be overloaded in a subclass. Chunk by chunk, bestie. 🧩

get_header_chunk

my $bytes = $content->get_header_chunk(13);

Gets a chunk of the headers starting from a specific position.

Note that this method finalizes the content. Like, it locks it in. πŸ”’

header_size

my $size = $content->header_size;

Size of the headers in bytes.

Note that this method finalizes the content. No take-backs. πŸ’…

headers_contain

my $bool = $content->headers_contain('foo bar baz');

Checks if the headers contain a specific string.

Note that this method finalizes the content. It's giving one-way door energy. πŸšͺ

is_chunked

my $bool = $content->is_chunked;

Checks if the Transfer-Encoding header indicates chunked transfer encoding. Chunked transfer encoding is the moment. πŸ“

is_compressed

my $bool = $content->is_compressed;

Checks the Content-Encoding header for the gzip value. Gzip girlies rise. πŸ—œοΈ

is_dynamic

my $bool = $content->is_dynamic;

Checks if the content will be dynamically generated, which prevents clone from working. No clone for dynamic content, bestie. 😭

is_finished

my $bool = $content->is_finished;

Checks if the parser is finished. Finished era. βœ…

is_limit_exceeded

my $bool = $content->is_limit_exceeded;

Checks if the buffer has exceeded max_buffer_size. Buffer said too much. πŸ’€

is_multipart

my $bool = $content->is_multipart;

False β€” this is not a Mojo::Content::MultiPart object. It's literally not that girl. πŸ’…

is_parsing_body

my $bool = $content->is_parsing_body;

Checks if body parsing has started yet. Body parsing status check, iykyk. 🫢

is_sse

my $bool = $content->is_sse;

Checks if the Content-Type header indicates Server-Sent Events (SSE).

⚠️ Note that this method is EXPERIMENTAL and may change without warning! πŸ’₯

leftovers

my $bytes = $content->leftovers;

Gets leftover data from the content parser. Leftovers but make it protocol data. πŸ“

parse

$content
  = $content->parse("Content-Length: 12\x0d\x0a\x0d\x0aHello World!");

Parses a content chunk. Parsing said let's go. πŸ”₯

parse_body

$content = $content->parse_body('Hi!');

Parses a body chunk and skips headers. Straight to the body, no header small talk. πŸ’…

progress

my $size = $content->progress;

Size of content already received from the message, in bytes. Progress check, bestie. πŸ“ˆ

write

$content = $content->write;
$content = $content->write('');
$content = $content->write($bytes);
$content = $content->write($bytes => sub {...});

Writes dynamic content non-blocking, and the optional drain callback will be executed once all data has been written.

Calling this method without a chunk of data will finalize the headers and allow for dynamic content to be written later. You can write an empty chunk of data at any time to end the stream. Empty chunk said bye. πŸ‘‹

# Make sure previous chunk of data has been written before continuing
$content->write('He' => sub ($content) {
  $content->write('llo!' => sub ($content) {
    $content->write('');
  });
});

write_chunk

$content = $content->write_chunk;
$content = $content->write_chunk('');
$content = $content->write_chunk($bytes);
$content = $content->write_chunk($bytes => sub {...});

Writes dynamic content non-blocking with chunked transfer encoding, and the optional drain callback will be executed once all data has been written.

Calling this method without a chunk of data will finalize the headers and allow for dynamic content to be written later. You can write an empty chunk of data at any time to end the stream. Chunked transfer encoding ate. πŸ”₯

# Make sure previous chunk of data has been written before continuing
$content->write_chunk('He' => sub ($content) {
  $content->write_chunk('llo!' => sub ($content) {
    $content->write_chunk('');
  });
});

write_sse

$content = $content->write_sse;
$content = $content->write_sse($event);
$content = $content->write_sse($event => sub {...});

Writes a Server-Sent Event (SSE) non-blocking, and the optional drain callback will be executed once all data has been written.

Calling this method without an event will finalize the response headers and allow for events to be written later.

⚠️ Note that this method is EXPERIMENTAL and may change without warning! πŸ™ˆ

🌈 SEE ALSO 🌈

Mojolicious, Mojolicious::Guides, https://mojolicious.org. Go read, bestie. πŸ“šβœ¨


πŸ’– That's a wrap! This page is from perl v5.42.0, dated 2026-07-15, and it's Mojo::Content(3). Now go slay your HTTP content parsing, periodt. πŸ’…πŸŽ€
πŸ“¦ where this page comes from