Name

SPVM::Mojo::Message::Request - HTTP request

Description

Mojo::Message::Request class in SPVM is a container for HTTP requests, based on RFC 7230, RFC 7231, RFC 7235 and RFC 2817.

Usage

use Mojo::Message::Request;

# Parse
my $req = Mojo::Message::Request->new;
$req->parse("GET /foo HTTP/1.0\x0d\x0a");
$req->parse("Content-Length: 12\x0d\x0a");
$req->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
$req->parse("Hello World!");
say $req->method;
say $req->headers->content_type;
say $req->body;

# Build
my $req = Mojo::Message::Request->new;
$req->url->parse("http://127.0.0.1/foo/bar");
$req->set_method("GET");
say $req->to_string;

Super Class

Mojo::Message

Fields

env

has env : rw Hash of string;

Direct access to the CGI or PSGI environment hash if available.

# Check CGI version
my $version = $req->env->get("GATEWAY_INTERFACE");

# Check PSGI version
my $version = $req->env->get("psgi.version");

method

has method : rw string;

HTTP request method, defaults to GET.

proxy

has proxy : rw Mojo::URL;

Proxy URL for request.

reverse_proxy

has reverse_proxy : rw byte;

Request has been performed through a reverse proxy.

trusted_proxies

has trusted_proxies : rw string[];

Trusted reverse proxies, addresses or networks in CIDR form.

request_id

has request_id : rw string;

Request ID, defaults to a reasonably unique value.

url

has url : rw Mojo::URL;

HTTP request URL, defaults to a Mojo::URL object.

# Get request information
my $info = $req->url->to_abs->userinfo;
my $host = $req->url->to_abs->host;
my $path = $req->url->to_abs->path;

via_proxy

has via_proxy : rw byte;

Request can be performed through a proxy server.

Class Methods

new

static method new : Mojo::Message::Request ();

Create a new Mojo::Message::Request object.

Instance Methods

clone

method clone : Mojo::Message::Request ();

Return a new Mojo::Message::Request object cloned from this request if possible, otherwise return undef.

cookies

method cookies : Mojo::Cookie::Request[] ();

Get request cookies, usually Mojo::Cookie::Request objects.

# Names of all cookies
for my $_ (@{$req->cookies}) {
  say $_->name ;
}

set_cookies

method set_cookies : void ($cookies : Mojo::Cookie::Request[]);

Set request cookies, usually Mojo::Cookie::Request objects.

every_param

method every_param : string[] ($name : string);

Similar to "param", but returns all values sharing the same name as an array reference.

# Get first value
say $req->every_param("foo")->[0];

extract_start_line

method extract_start_line : int ($bufref : string[]);

Extract request-line from string.

fix_headers

method fix_headers : void ();

Make sure request has all required headers.

get_start_line_chunk

method get_start_line_chunk : string ($offset : int);

Get a chunk of request-line data starting from a specific position. Note that this method finalizes the request.

is_handshake

method is_handshake : int ();

Check Upgrade header for websocket value.

is_secure

method is_secure : int ();

Check if connection is secure.

is_xhr

method is_xhr : int ();

Check X-Requested-With header for XMLHttpRequest value.

param

method param : string ($name : string);

Access GET and POST parameters extracted from the query string and application/x-www-form-urlencoded or multipart/form-data message body. If there are multiple values sharing the same name, and you want to access more than just the last one, you can use "every_param". Note that this method caches all data, so it should not be called before the entire request body has been received. Parts of the request body need to be loaded into memory to parse POST parameters, so you have to make sure it is not excessively large. There's a 16MiB limit for requests by default.

params

method params : Mojo::Parameters ();

All GET and POST parameters extracted from the query string and application/x-www-form-urlencoded or multipart/form-data message body, usually a Mojo::Parameters object. Note that this method caches all data, so it should not be called before the entire request body has been received. Parts of the request body need to be loaded into memory to parse POST parameters, so you have to make sure it is not excessively large. There's a 16MiB limit for requests by default.

# Get parameter names and values
my $hash = $req->params->to_hash;

parse

method parse : void ($chunk : string, $env : Hash of string = undef);

Parse HTTP request chunks or environment hash.

query_params

method query_params : Mojo::Parameters ();

All GET parameters, usually a Mojo::Parameters object.

# Turn GET parameters to hash and extract value
say $req->query_params->to_hash->get("foo");

See Also

Copyright & License

Copyright (c) 2025 Yuki Kimoto

MIT License