NAME

PAGI::Context::HTTP - HTTP-specific context subclass

DESCRIPTION

Returned by PAGI::Context->new(...) when $scope->{type} is 'http'. Adds lazy accessors for PAGI::Request and PAGI::Response, plus an HTTP method accessor.

Inherits all shared methods from PAGI::Context.

METHODS

request

my $req = $ctx->request;

Returns a PAGI::Request instance. Lazy-constructed and cached.

response

my $res = $ctx->response;

Returns a detached PAGI::Response accumulator. Lazy-constructed and cached for the lifetime of the context. The response holds no connection — it is a pure value object you mutate via the chainer methods (status, header, json, etc.) and then pass to "respond" when ready to send.

respond

$ctx->respond($res);

Guarded send. Sends the PAGI::Response value $res over this request's connection, marks the request as done, and returns a Future that resolves when all protocol events have been emitted.

The sent state is read from $scope->{'pagi.connection'}->response_started, the server-seeded per-request object — a shared reference, so the fact propagates across the whole middleware stack even though middleware shallow-clone the scope. A second respond on the same context is rejected synchronously via a per-context flag; the server enforces single-response at the protocol level as the backstop.

Delegates to the unguarded primitive $res->respond($send).

method

my $method = $ctx->method;    # 'GET', 'POST', etc.

Returns the HTTP method from the scope.

req

my $req = $ctx->req;

Alias for request.

resp

my $res = $ctx->resp;

Alias for response.

text, html, json, redirect

return $ctx->text('Hello');
return $ctx->html('<h1>Hi</h1>');
return $ctx->json({ ok => 1 });
return $ctx->json($data, status => 201);
return $ctx->redirect('/login');

Shorthands that set the body — and any trailing options such as status, headers, or content_type — on this context's "response", then return the PAGI::Response value, so a handler can build and return its response in a single call. $ctx->json($data) is exactly $ctx->response->json($data). They operate on the one response accumulator, so you can still reach for $ctx->response to set cookies or extra headers alongside them.

SEE ALSO

PAGI::Context, PAGI::Request, PAGI::Response