Kelp
view release on metacpan or search on metacpan
lib/Kelp/Response.pm view on Meta::CPAN
The charset to be used in response. Will be glued to C<Content-Type> header
just before the response is finalized.
NOTE: charset will be glued regardless of it having any sense with a given
C<Content-Type>, and will override any charset set explicitly through
L</set_content_type> - use with caution.
=head2 rendered
Tells if the response has been rendered. This attribute is used internally and
unless you know what you're doing, we recommend that you do not use it.
=head2 partial
Sets partial response. If this attribute is set to a true value, it will cause
C<finalize> to return the HTTP status code and headers, but not the body. This is
convenient if you intend to stream your content. In the following example, we
set C<partial> to 1 and use C<finalize> to get a C<writer> object for streaming.
sub stream {
my $self = shift;
return sub {
my $responder = shift;
# Stream JSON
$self->res->set_code(200)->json->partial(1);
# finalize will now return only the status code and headers
my $writer = $responder->($self->res->finalize);
# Stream JSON body using the writer object
for (1 .. 30) {
$writer->write(qq|{"id":$_}\n|);
sleep 1;
}
# Close the writer
$writer->close;
};
}
For more information on how to stream, see the
L<PSGI/Delayed-Response-and-Streaming-Body> docs.
=head1 METHODS
=head2 render
This method tries to act smart, without being a control freak. It will fill out
the blanks, unless they were previously filled out by someone else. Here is what
is does:
=over
=item
If the response code was not previously set, this method will set it to 200.
=item
If no content-type is previously set, C<render> will set is based on the type
of the data rendered. If it's a reference, then the content-type will be set to
C<application/json>, otherwise it will be set to C<text/html>.
# Will set the content-type to json
$res->render({ numbers => [1, 2, 3] });
=item
Last, the data will be encoded with the charset from L</charset> or the one
specified by the app - see L<Kelp/charset>. Any string you pass here should not
already be encoded, unless your application has its charset set to undef.
=back
=head2 set_content_type
Sets the content type of the response and returns C<$self>.
# Inside a route definition
$self->res->set_content_type('image/png');
An optional second argument can be passed, which will be used for C<charset>
part of C<Content-Type> (will set L</charset> field).
=head2 text, html, json, xml
These methods are shortcuts for C<set_content_type> with the corresponding type.
All of them set the content-type header and return C<$self> so they can be
chained.
$self->res->text->render("word");
$self->res->html->render("<p>word</p>");
$self->res->json->render({ word => \1 });
NOTE: These methods will also call L</charset> and set it to application's
charset (unless it was previously set).
=head2 set_header
Sets response headers. This is a wrapper around L<Plack::Response/header>, which
returns C<$self> to allow for chaining.
$self->res->set_header('X-Something' => 'Value')->text->render("Hello");
=head2 no_cache
A convenience method that sets several response headers instructing most
browsers to not cache the response.
$self->res->no_cache->json->render({ epoch => time });
The above response will contain headers that disable caching.
=head2 set_code
Set the response code.
$self->res->set_code(401)->render("Access denied");
=head2 render_binary
Render binary data such as byte streams, files, images, etc. You must
explicitly set the content_type before that. Will not encode the content into
any charset.
use Kelp::Less;
get '/image/:name' => sub {
my $content = Path::Tiny::path("$name.jpg")->slurp_raw;
res->set_content_type('image/jpeg')->render_binary($content);
# the same, but probably more effective way (PSGI-server dependent)
open my $handle, "<:raw", "$name.png"
or die "cannot open $name: $!";
res->set_content_type('image/png')->render_binary($handle);
};
=head2 render_error
$self->render_error($code, $error)
Renders the specified return code and an error message. This sub will first look
for this error template C<error/$code>, before displaying a plain page with the
error text.
$self->res->render_error(510, "Not Extended");
The above code will look for a template named C<views/errors/510.tt>, and if not
( run in 0.971 second using v1.01-cache-2.11-cpan-d7f47b0818f )