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::Fast->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::Fast object containing a
48 list 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 "max-age".
102
103 "expires" can take a string or an integer (as an epoch time) and
104 does not convert string formats such as "+3M".
105
106 $res->cookies->{foo} = {
107 value => 'test',
108 path => "/",
109 domain => '.example.com',
110 expires => time + 24 * 60 * 60,
111 };
112
113 finalize
114 $res->finalize;
115
116 Returns the status code, headers, and body of this response as a
117 PSGI response array reference.
118
119 to_app
120 $app = $res->to_app;
121
122 A helper shortcut for "sub { $res->finalize }".
123
125 Tokuhiro Matsuno
126
127 Tatsuhiko Miyagawa
128
130 Plack::Request
131
132
133
134perl v5.30.0 2019-07-26 Plack::Response(3)