NAME

PAGI::Nano::Context::HTTP - The HTTP request context vended by PAGI::Nano

SYNOPSIS

get '/' => sub ($c) {
    my $name = $c->req->query_param('name');
    $c->json({ hello => $name });
};

post '/tasks' => sub ($c) {
    my $attrs = await $c->params->required(
        'title',
        sub ($c, $missing) { $c->json({ error => 'missing', fields => $missing }, status => 400) },
    );
    ...
};

DESCRIPTION

A subclass of PAGI::Context::HTTP, so every method of the base HTTP context is available — req/request, response/resp, respond, method, state, path_param, and the response sugar json/text/html/ redirect. A Nano handler receives one of these as $c.

The additions are "params" and "uri_for". The inherited $c->raw_send (the raw PAGI $send coderef; see "raw_send" in PAGI::Context) is also available for handlers that drop to the channel directly.

METHODS

params

my $params = $c->params;            # PAGI::StructuredParameters::Request
my $clean  = await $params->permitted(@rules);
my $clean  = await $params->required(@rules, $on_missing);

The strong-parameters entry point. Returns a PAGI::StructuredParameters::Request bound to the current request, selecting the source by content-type: a JSON request uses from_data (nested data, arrays kept as-is); any other request uses from_body (form parameters, array values flattened). The context $c is passed through so required's on-missing callback receives it as its first argument.

Because reading a request body is asynchronous, the terminal permitted and required calls must be awaited.

uri_for

my $url = $c->uri_for($name);
my $url = $c->uri_for($name, \%path_params);
my $url = $c->uri_for($name, \%path_params, \%query_params);

Builds the URL for a route named with "name" in PAGI::Nano. Path placeholders (:id, {id}, {id:regex}, *splat) are filled from %path_params, and %query_params (if any) is appended as a percent-encoded query string. Resolution is against the flat name registry PAGI::Nano injects on the scope, so names defined anywhere in the app — including across a mount, in either direction — are reachable, with mount prefixes applied. Dies if the name is unknown.

SEE ALSO

PAGI::Nano, PAGI::StructuredParameters, PAGI::Context::HTTP.

AUTHOR

John Napiorkowski <jjnapiork@cpan.org>

COPYRIGHT & LICENSE

Copyright 2026, John Napiorkowski. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.