1Plack::Response(3) User Contributed Perl Documentation Plack::Response(3)
2
3
4
6 Plack::Response - Portable HTTP Response object for PSGI response
7
9 use Plack::Response;
10
11 sub psgi_handler {
12 my $env = shift;
13
14 my $res = Plack::Response->new(200);
15 $res->content_type('text/html');
16 $res->body("Hello World");
17
18 return $res->finalize;
19 }
20
22 Plack::Response allows you a way to create PSGI response array ref
23 through a simple API.
24
26 new
27 $res = Plack::Response->new;
28 $res = Plack::Response->new($status);
29 $res = Plack::Response->new($status, $headers);
30 $res = Plack::Response->new($status, $headers, $body);
31
32 Creates a new Plack::Response object.
33
34 status
35 $res->status(200);
36 $status = $res->status;
37
38 Sets and gets HTTP status code. "code" is an alias.
39
40 headers
41 $headers = $res->headers;
42 $res->headers([ 'Content-Type' => 'text/html' ]);
43 $res->headers({ 'Content-Type' => 'text/html' });
44 $res->headers( HTTP::Headers->new );
45
46 Sets and gets HTTP headers of the response. Setter can take either
47 an array ref, a hash ref or HTTP::Headers object containing a list
48 of headers.
49
50 body
51 $res->body($body_str);
52 $res->body([ "Hello", "World" ]);
53 $res->body($io);
54
55 Gets and sets HTTP response body. Setter can take either a string,
56 an array ref, or an IO::Handle-like object. "content" is an alias.
57
58 Note that this method doesn't automatically set Content-Length for
59 the response. You have to set it manually if you want, with the
60 "content_length" method (see below).
61
62 header
63 $res->header('X-Foo' => 'bar');
64 my $val = $res->header('X-Foo');
65
66 Shortcut for "$res->headers->header".
67
68 content_type, content_length, content_encoding
69 $res->content_type('text/plain');
70 $res->content_length(123);
71 $res->content_encoding('gzip');
72
73 Shortcut for the equivalent get/set methods in "$res->headers".
74
75 redirect
76 $res->redirect($url);
77 $res->redirect($url, 301);
78
79 Sets redirect URL with an optional status code, which defaults to
80 302.
81
82 Note that this method doesn't normalize the given URI string. Users
83 of this module have to be responsible about properly encoding URI
84 paths and parameters.
85
86 location
87 Gets and sets "Location" header.
88
89 Note that this method doesn't normalize the given URI string in the
90 setter. See above in "redirect" for details.
91
92 cookies
93 $res->cookies->{foo} = 123;
94 $res->cookies->{foo} = { value => '123' };
95
96 Returns a hash reference containing cookies to be set in the
97 response. The keys of the hash are the cookies' names, and their
98 corresponding values are a plain string (for "value" with
99 everything else defaults) or a hash reference that can contain keys
100 such as "value", "domain", "expires", "path", "httponly", "secure".
101
102 "expires" can take a string or an integer (as an epoch time) and
103 does not convert string formats such as "+3M".
104
105 $res->cookies->{foo} = {
106 value => 'test',
107 path => "/",
108 domain => '.example.com',
109 expires => time + 24 * 60 * 60,
110 };
111
112 finalize
113 $res->finalize;
114
115 Returns the status code, headers, and body of this response as a
116 PSGI response array reference.
117
119 Tokuhiro Matsuno
120
121 Tatsuhiko Miyagawa
122
124 Plack::Request
125
126
127
128perl v5.12.3 2011-07-19 Plack::Response(3)