1HTTP::Response(3) User Contributed Perl Documentation HTTP::Response(3)
2
3
4
6 HTTP::Response - HTTP style response message
7
9 version 6.44
10
12 Response objects are returned by the request() method of the
13 "LWP::UserAgent":
14
15 # ...
16 $response = $ua->request($request);
17 if ($response->is_success) {
18 print $response->decoded_content;
19 }
20 else {
21 print STDERR $response->status_line, "\n";
22 }
23
25 The "HTTP::Response" class encapsulates HTTP style responses. A
26 response consists of a response line, some headers, and a content body.
27 Note that the LWP library uses HTTP style responses even for non-HTTP
28 protocol schemes. Instances of this class are usually created and
29 returned by the request() method of an "LWP::UserAgent" object.
30
31 "HTTP::Response" is a subclass of "HTTP::Message" and therefore
32 inherits its methods. The following additional methods are available:
33
34 $r = HTTP::Response->new( $code )
35 $r = HTTP::Response->new( $code, $msg )
36 $r = HTTP::Response->new( $code, $msg, $header )
37 $r = HTTP::Response->new( $code, $msg, $header, $content )
38 Constructs a new "HTTP::Response" object describing a response with
39 response code $code and optional message $msg. The optional
40 $header argument should be a reference to an "HTTP::Headers" object
41 or a plain array reference of key/value pairs. The optional
42 $content argument should be a string of bytes. The meanings of
43 these arguments are described below.
44
45 $r = HTTP::Response->parse( $str )
46 This constructs a new response object by parsing the given string.
47
48 $r->code
49 $r->code( $code )
50 This is used to get/set the code attribute. The code is a 3 digit
51 number that encode the overall outcome of an HTTP response. The
52 "HTTP::Status" module provide constants that provide mnemonic names
53 for the code attribute.
54
55 $r->message
56 $r->message( $message )
57 This is used to get/set the message attribute. The message is a
58 short human readable single line string that explains the response
59 code.
60
61 $r->header( $field )
62 $r->header( $field => $value )
63 This is used to get/set header values and it is inherited from
64 "HTTP::Headers" via "HTTP::Message". See HTTP::Headers for details
65 and other similar methods that can be used to access the headers.
66
67 $r->content
68 $r->content( $bytes )
69 This is used to get/set the raw content and it is inherited from
70 the "HTTP::Message" base class. See HTTP::Message for details and
71 other methods that can be used to access the content.
72
73 $r->decoded_content( %options )
74 This will return the content after any "Content-Encoding" and
75 charsets have been decoded. See HTTP::Message for details.
76
77 $r->request
78 $r->request( $request )
79 This is used to get/set the request attribute. The request
80 attribute is a reference to the request that caused this response.
81 It does not have to be the same request passed to the
82 $ua->request() method, because there might have been redirects and
83 authorization retries in between.
84
85 $r->previous
86 $r->previous( $response )
87 This is used to get/set the previous attribute. The previous
88 attribute is used to link together chains of responses. You get
89 chains of responses if the first response is redirect or
90 unauthorized. The value is "undef" if this is the first response
91 in a chain.
92
93 Note that the method $r->redirects is provided as a more convenient
94 way to access the response chain.
95
96 $r->status_line
97 Returns the string "<code> <message>". If the message attribute is
98 not set then the official name of <code> (see HTTP::Status) is
99 substituted.
100
101 $r->base
102 Returns the base URI for this response. The return value will be a
103 reference to a URI object.
104
105 The base URI is obtained from one the following sources (in
106 priority order):
107
108 1. Embedded in the document content, for instance <BASE
109 HREF="..."> in HTML documents.
110
111 2. A "Content-Base:" header in the response.
112
113 For backwards compatibility with older HTTP implementations we
114 will also look for the "Base:" header.
115
116 3. The URI used to request this response. This might not be the
117 original URI that was passed to $ua->request() method, because
118 we might have received some redirect responses first.
119
120 If none of these sources provide an absolute URI, undef is
121 returned.
122
123 Note: previous versions of HTTP::Response would also consider a
124 "Content-Location:" header, as RFC 2616 <https://www.rfc-
125 editor.org/rfc/rfc2616> said it should be. But this was never
126 widely implemented by browsers, and now RFC 7231 <https://www.rfc-
127 editor.org/rfc/rfc7231> says it should no longer be considered.
128
129 When the LWP protocol modules produce the HTTP::Response object,
130 then any base URI embedded in the document (step 1) will already
131 have initialized the "Content-Base:" header. (See "parse_head" in
132 LWP::UserAgent). This means that this method only performs the
133 last 2 steps (the content is not always available either).
134
135 $r->filename
136 Returns a filename for this response. Note that doing sanity
137 checks on the returned filename (eg. removing characters that
138 cannot be used on the target filesystem where the filename would be
139 used, and laundering it for security purposes) are the caller's
140 responsibility; the only related thing done by this method is that
141 it makes a simple attempt to return a plain filename with no
142 preceding path segments.
143
144 The filename is obtained from one the following sources (in
145 priority order):
146
147 1. A "Content-Disposition:" header in the response. Proper
148 decoding of RFC 2047 encoded filenames requires the
149 "MIME::QuotedPrint" (for "Q" encoding), "MIME::Base64" (for "B"
150 encoding), and "Encode" modules.
151
152 2. A "Content-Location:" header in the response.
153
154 3. The URI used to request this response. This might not be the
155 original URI that was passed to $ua->request() method, because
156 we might have received some redirect responses first.
157
158 If a filename cannot be derived from any of these sources, undef is
159 returned.
160
161 $r->as_string
162 $r->as_string( $eol )
163 Returns a textual representation of the response.
164
165 $r->is_info
166 $r->is_success
167 $r->is_redirect
168 $r->is_error
169 $r->is_client_error
170 $r->is_server_error
171 These methods indicate if the response was informational,
172 successful, a redirection, or an error. See HTTP::Status for the
173 meaning of these.
174
175 $r->error_as_HTML
176 Returns a string containing a complete HTML document indicating
177 what error occurred. This method should only be called when
178 $r->is_error is TRUE.
179
180 $r->redirects
181 Returns the list of redirect responses that lead up to this
182 response by following the $r->previous chain. The list order is
183 oldest first.
184
185 In scalar context return the number of redirect responses leading
186 up to this one.
187
188 $r->current_age
189 Calculates the "current age" of the response as specified by RFC
190 2616 section 13.2.3. The age of a response is the time since it
191 was sent by the origin server. The returned value is a number
192 representing the age in seconds.
193
194 $r->freshness_lifetime( %opt )
195 Calculates the "freshness lifetime" of the response as specified by
196 RFC 2616 section 13.2.4. The "freshness lifetime" is the length of
197 time between the generation of a response and its expiration time.
198 The returned value is the number of seconds until expiry.
199
200 If the response does not contain an "Expires" or a "Cache-Control"
201 header, then this function will apply some simple heuristic based
202 on the "Last-Modified" header to determine a suitable lifetime.
203 The following options might be passed to control the heuristics:
204
205 heuristic_expiry => $bool
206 If passed as a FALSE value, don't apply heuristics and just
207 return "undef" when "Expires" or "Cache-Control" is lacking.
208
209 h_lastmod_fraction => $num
210 This number represent the fraction of the difference since the
211 "Last-Modified" timestamp to make the expiry time. The default
212 is 0.10, the suggested typical setting of 10% in RFC 2616.
213
214 h_min => $sec
215 This is the lower limit of the heuristic expiry age to use.
216 The default is 60 (1 minute).
217
218 h_max => $sec
219 This is the upper limit of the heuristic expiry age to use.
220 The default is 86400 (24 hours).
221
222 h_default => $sec
223 This is the expiry age to use when nothing else applies. The
224 default is 3600 (1 hour) or "h_min" if greater.
225
226 $r->is_fresh( %opt )
227 Returns TRUE if the response is fresh, based on the values of
228 freshness_lifetime() and current_age(). If the response is no
229 longer fresh, then it has to be re-fetched or re-validated by the
230 origin server.
231
232 Options might be passed to control expiry heuristics, see the
233 description of freshness_lifetime().
234
235 $r->fresh_until( %opt )
236 Returns the time (seconds since epoch) when this entity is no
237 longer fresh.
238
239 Options might be passed to control expiry heuristics, see the
240 description of freshness_lifetime().
241
243 HTTP::Headers, HTTP::Message, HTTP::Status, HTTP::Request
244
246 Gisle Aas <gisle@activestate.com>
247
249 This software is copyright (c) 1994 by Gisle Aas.
250
251 This is free software; you can redistribute it and/or modify it under
252 the same terms as the Perl 5 programming language system itself.
253
254
255
256perl v5.36.0 2023-01-20 HTTP::Response(3)