response

Response

MVC::Keayl::Response is a mutable builder for a single outgoing HTTP response. Controllers and middleware set the status, headers, and body on it; the server adapter then calls finish to obtain the (status, headers, body) tuple it hands back to the connection.

use MVC::Keayl::Response;

my $response = MVC::Keayl::Response.new;

$response.status = 201;
$response.content-type('application/json');
$response.location('/users/1');
$response.write('{"id":1}');

my ($status, $headers, $body) = $response.finish;

Construction

Named argumentMeaning
:statusHTTP status code. Defaults to 200.
:headersA hash of header names to initial values.
:bodyThe initial body content.

Status

status is a writable accessor:

$response.status;        # 200
$response.status = 404;

Headers

Header names are matched case-insensitively, and the display casing you set is preserved on output.

$response.set-header('Content-Type', 'text/plain');   # set / replace
$response.add-header('Set-Cookie', 'a=1');            # append another value
$response.add-header('Set-Cookie', 'b=2');

$response.header('content-type');   # 'text/plain'
$response.header('set-cookie');     # 'a=1, b=2'  (values joined for reading)

$response.has-header('Content-Type');   # True
$response.delete-header('Content-Type');

$response.headers;   # { 'Set-Cookie' => 'a=1, b=2' }  (display names, joined values)

Convenience accessors

content-type and location are getter/setter shortcuts for their headers, and content-length reports the body's UTF-8 byte count:

$response.content-type('application/json');
$response.content-type;     # 'application/json'

$response.location('/users/1');
$response.location;         # '/users/1'

$response.content-length;   # byte count of the current body

Body buffering

The body accumulates across write calls; the body getter returns the joined buffer, and the body setter replaces it:

$response.write('Hello, ');
$response.write('world');
$response.body;             # 'Hello, world'

$response.body('replaced'); # discards the buffer

Finalization

finish commits the response and returns the adapter tuple: a (status, headers, body) list where headers is an itemized list of name => value pairs (one pair per value, so multi-value headers like Set-Cookie stay separate) and body is a UTF-8 Blob:

my ($status, $headers, $body) = $response.finish;

$status;          # 201
$headers.list;    # (Content-Type => ..., Content-Length => ..., ...)
$body;            # Blob

On finalization the response supplies a default Content-Type of text/html; charset=utf-8 when none was set, and always sets Content-Length to the body's byte count.

MVC::Keayl v0.9.1

Model-View-Controller web framework

Authors

  • Greg Donald

License

Artistic-2.0

Dependencies

Cro::HTTPCrypt::RandomDigest::HMACDigest::SHA1::NativeOpenSSLMIME::Base64YAMLishORM::ActiveRecord:auth<zef:gdonald>Template::HAML:auth<zef:gdonald>

Provides

  • MVC::Keayl
  • MVC::Keayl::APIController
  • MVC::Keayl::ActionText
  • MVC::Keayl::ActionText::Repository
  • MVC::Keayl::ActionText::RichTextable
  • MVC::Keayl::Adapter
  • MVC::Keayl::Adapter::Cro
  • MVC::Keayl::Adapter::Test
  • MVC::Keayl::Application
  • MVC::Keayl::Assets
  • MVC::Keayl::Assets::Serving
  • MVC::Keayl::CLI
  • MVC::Keayl::CSRF
  • MVC::Keayl::Cable::Broadcasting
  • MVC::Keayl::Cable::Channel
  • MVC::Keayl::Cable::Connection
  • MVC::Keayl::Cable::PubSub
  • MVC::Keayl::Cable::PubSub::External
  • MVC::Keayl::Cable::PubSub::InMemory
  • MVC::Keayl::Cache
  • MVC::Keayl::Caching
  • MVC::Keayl::Config
  • MVC::Keayl::Controller
  • MVC::Keayl::Cookies
  • MVC::Keayl::Credentials
  • MVC::Keayl::Dispatcher
  • MVC::Keayl::Endpoint
  • MVC::Keayl::Engine
  • MVC::Keayl::ErrorReporter
  • MVC::Keayl::ErrorReporting
  • MVC::Keayl::Errors
  • MVC::Keayl::ExceptionPage
  • MVC::Keayl::Flash
  • MVC::Keayl::HealthController
  • MVC::Keayl::Helpers::Asset
  • MVC::Keayl::Helpers::DateTime
  • MVC::Keayl::Helpers::Form
  • MVC::Keayl::Helpers::Number
  • MVC::Keayl::Helpers::Options
  • MVC::Keayl::Helpers::Tag
  • MVC::Keayl::Helpers::Text
  • MVC::Keayl::Helpers::Url
  • MVC::Keayl::HttpAuthentication
  • MVC::Keayl::I18n
  • MVC::Keayl::I18n::Locale
  • MVC::Keayl::I18n::Pluralization
  • MVC::Keayl::Job
  • MVC::Keayl::Job::GlobalID
  • MVC::Keayl::Job::QueueAdapter
  • MVC::Keayl::Job::QueueAdapter::Async
  • MVC::Keayl::Job::QueueAdapter::Database
  • MVC::Keayl::Job::QueueAdapter::Inline
  • MVC::Keayl::Job::QueueAdapter::Test
  • MVC::Keayl::Live
  • MVC::Keayl::LogEvent
  • MVC::Keayl::Logger
  • MVC::Keayl::Mail
  • MVC::Keayl::Mailbox
  • MVC::Keayl::Mailbox::Ingress
  • MVC::Keayl::Mailbox::Router
  • MVC::Keayl::Mailer
  • MVC::Keayl::Mailer::Delivery
  • MVC::Keayl::Mailer::Delivery::File
  • MVC::Keayl::Mailer::Delivery::SMTP
  • MVC::Keayl::Mailer::Delivery::Test
  • MVC::Keayl::Mailer::DeliveryJob
  • MVC::Keayl::Mailer::Preview
  • MVC::Keayl::Middleware
  • MVC::Keayl::Middleware::Database
  • MVC::Keayl::Middleware::HostAuthorization
  • MVC::Keayl::Middleware::Logger
  • MVC::Keayl::Middleware::RequestId
  • MVC::Keayl::Middleware::SSL
  • MVC::Keayl::Middleware::SecureHeaders
  • MVC::Keayl::Middleware::Static
  • MVC::Keayl::MiddlewareStack
  • MVC::Keayl::Mime
  • MVC::Keayl::Notifications
  • MVC::Keayl::PWAController
  • MVC::Keayl::ParameterFilter
  • MVC::Keayl::Parameters
  • MVC::Keayl::Params
  • MVC::Keayl::Request
  • MVC::Keayl::Response
  • MVC::Keayl::Router
  • MVC::Keayl::Routing
  • MVC::Keayl::Routing::Mount
  • MVC::Keayl::Routing::PathPattern
  • MVC::Keayl::Routing::Redirect
  • MVC::Keayl::Routing::Resources
  • MVC::Keayl::Routing::Route
  • MVC::Keayl::Routing::RouteMatch
  • MVC::Keayl::Routing::UrlHelpers
  • MVC::Keayl::SafeString
  • MVC::Keayl::Secrets
  • MVC::Keayl::Session
  • MVC::Keayl::Storage
  • MVC::Keayl::Storage::Attached
  • MVC::Keayl::Storage::Repository
  • MVC::Keayl::Storage::Service
  • MVC::Keayl::Storage::Serving
  • MVC::Keayl::Storage::Variant
  • MVC::Keayl::TestSupport
  • MVC::Keayl::View
  • MVC::Keayl::View::Context
  • MVC::Keayl::View::Handler
  • MVC::Keayl::View::Handler::HAML

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.