Name
SPVM::Mojo::Message::Response - HTTP response
Description
Mojo::Message::Response class in SPVM is a container for HTTP responses, based on RFC 7230 and RFC 7231.
Usage
use Mojo::Message::Response;
# Parse
my $res = Mojo::Message::Response->new;
$res->parse("HTTP/1.0 200 OK\x0d\x0a");
$res->parse("Content-Length: 12\x0d\x0a");
$res->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
$res->parse('Hello World!');
say $res->code;
say $res->headers->content_type;
say $res->body;
# Build
my $res = Mojo::Message::Response->new;
$res->set_code(200);
$res->headers->set_content_type("text/plain");
$res->set_body("Hello World!");
say $res->to_string;
Inheritance
Fields
code
has code: rw int;
HTTP response status code.
max_message_size
has max_message_size : rw int
Maximum message size in bytes, defaults to the value of the SPVM_MOJO_MAX_MESSAGE_SIZE
environment variable or 2147483648
(2GiB). Setting the value to 0
will allow messages of indefinite size.
message
has message : rw string;
HTTP response status message.
Class Methods
new
static method new : Mojo::Message::Response ();
Create a new Mojo::Message::Response object and return it.
Instance Methods
cookies
method cookies : Mojo::Cookie::Response[] ();
Access response cookies, usually Mojo::Cookie::Response objects.
# Names of all cookies
for my $_ (@{$res->cookies}) {
say $_->name;
}
default_message
method default_message : string ($code : int = -1);
Generate default response message for status code, defaults to using "code".
extract_start_line
method extract_start_line : int ($bufref : string[]);
Extract status-line from string.
fix_headers
method fix_headers : void ();
Make sure response has all required headers.
get_start_line_chunk
method get_start_line_chunk : string ($offset : int);
Get a chunk of status-line data starting from a specific position. Note that this method finalizes the response.
is_client_error
method is_client_error : int ($code : int);
Check if this response has a 4xx
status "code".
is_empty
method is_empty : int ();
Check if this response has a 1xx
, 204
or 304
status "code".
is_error
method is_error : int ();
Check if this response has a 4xx
or 5xx
status "code".
is_info
method is_info : int ();
Check if this response has a 1xx
status "code".
is_redirect
method is_redirect : int ();
Check if this response has a 3xx
status "code".
is_server_error
method is_server_error : int ();
Check if this response has a 5xx
status "code".
is_success
method is_success : int ();
Check if this response has a 2xx
status "code".
start_line_size
method start_line_size : int ();
Size of the status-line in bytes. Note that this method finalizes the response.
See Also
Copyright & License
Copyright (c) 2025 Yuki Kimoto
MIT License