Plack::Response(3) Portable HTTP Response object for PSGI response


use Plack::Response;
sub psgi_handler {
my $env = shift;
my $res = Plack::Response->new(200);
$res->body("Hello World");
return $res->finalize;


Plack::Response allows you a way to create PSGI response array ref through a simple API.


  $res = Plack::Response->new;
  $res = Plack::Response->new($status);
  $res = Plack::Response->new($status, $headers);
  $res = Plack::Response->new($status, $headers, $body);

Creates a new Plack::Response object.

  $status = $res->status;

Sets and gets HTTP status code. "code" is an alias.

  $headers = $res->headers;
  $res->headers([ 'Content-Type' => 'text/html' ]);
  $res->headers({ 'Content-Type' => 'text/html' });
  $res->headers( HTTP::Headers::Fast->new );

Sets and gets HTTP headers of the response. Setter can take either an array ref, a hash ref or HTTP::Headers::Fast object containing a list of headers.

  $res->body([ "Hello", "World" ]);

Gets and sets HTTP response body. Setter can take either a string, an array ref, or an IO::Handle-like object. "content" is an alias.

Note that this method doesn't automatically set Content-Length for the response. You have to set it manually if you want, with the "content_length" method (see below).

  $res->header('X-Foo' => 'bar');
  my $val = $res->header('X-Foo');

Shortcut for "$res->headers->header".

content_type, content_length, content_encoding

Shortcut for the equivalent get/set methods in "$res->headers".

  $res->redirect($url, 301);

Sets redirect URL with an optional status code, which defaults to 302.

Note that this method doesn't normalize the given URI string. Users of this module have to be responsible about properly encoding URI paths and parameters.

Gets and sets "Location" header.

Note that this method doesn't normalize the given URI string in the setter. See above in "redirect" for details.

  $res->cookies->{foo} = 123;
  $res->cookies->{foo} = { value => '123' };

Returns a hash reference containing cookies to be set in the response. The keys of the hash are the cookies' names, and their corresponding values are a plain string (for "value" with everything else defaults) or a hash reference that can contain keys such as "value", "domain", "expires", "path", "httponly", "secure", "max-age".

"expires" can take a string or an integer (as an epoch time) and does not convert string formats such as "+3M".

  $res->cookies->{foo} = {
      value => 'test',
      path  => "/",
      domain => '',
      expires => time + 24 * 60 * 60,

Returns the status code, headers, and body of this response as a PSGI response array reference.

  $app = $res->to_app;

A helper shortcut for "sub { $res->finalize }".


Tokuhiro Matsuno

Tatsuhiko Miyagawa