1HTTP::Response(3) User Contributed Perl Documentation HTTP::Response(3)
2
3
4
6 HTTP::Response - HTTP style response message
7
9 version 6.18
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:" or a "Content-Location:" header in the
112 response.
113
114 For backwards compatibility with older HTTP implementations we
115 will also look for the "Base:" header.
116
117 3. The URI used to request this response. This might not be the
118 original URI that was passed to $ua->request() method, because
119 we might have received some redirect responses first.
120
121 If none of these sources provide an absolute URI, undef is
122 returned.
123
124 When the LWP protocol modules produce the HTTP::Response object,
125 then any base URI embedded in the document (step 1) will already
126 have initialized the "Content-Base:" header. (See "parse_head" in
127 LWP::UserAgent). This means that this method only performs the
128 last 2 steps (the content is not always available either).
129
130 $r->filename
131 Returns a filename for this response. Note that doing sanity
132 checks on the returned filename (eg. removing characters that
133 cannot be used on the target filesystem where the filename would be
134 used, and laundering it for security purposes) are the caller's
135 responsibility; the only related thing done by this method is that
136 it makes a simple attempt to return a plain filename with no
137 preceding path segments.
138
139 The filename is obtained from one the following sources (in
140 priority order):
141
142 1. A "Content-Disposition:" header in the response. Proper
143 decoding of RFC 2047 encoded filenames requires the
144 "MIME::QuotedPrint" (for "Q" encoding), "MIME::Base64" (for "B"
145 encoding), and "Encode" modules.
146
147 2. A "Content-Location:" header in the response.
148
149 3. The URI used to request this response. This might not be the
150 original URI that was passed to $ua->request() method, because
151 we might have received some redirect responses first.
152
153 If a filename cannot be derived from any of these sources, undef is
154 returned.
155
156 $r->as_string
157 $r->as_string( $eol )
158 Returns a textual representation of the response.
159
160 $r->is_info
161 $r->is_success
162 $r->is_redirect
163 $r->is_error
164 $r->is_client_error
165 $r->is_server_error
166 These methods indicate if the response was informational,
167 successful, a redirection, or an error. See HTTP::Status for the
168 meaning of these.
169
170 $r->error_as_HTML
171 Returns a string containing a complete HTML document indicating
172 what error occurred. This method should only be called when
173 $r->is_error is TRUE.
174
175 $r->redirects
176 Returns the list of redirect responses that lead up to this
177 response by following the $r->previous chain. The list order is
178 oldest first.
179
180 In scalar context return the number of redirect responses leading
181 up to this one.
182
183 $r->current_age
184 Calculates the "current age" of the response as specified by RFC
185 2616 section 13.2.3. The age of a response is the time since it
186 was sent by the origin server. The returned value is a number
187 representing the age in seconds.
188
189 $r->freshness_lifetime( %opt )
190 Calculates the "freshness lifetime" of the response as specified by
191 RFC 2616 section 13.2.4. The "freshness lifetime" is the length of
192 time between the generation of a response and its expiration time.
193 The returned value is the number of seconds until expiry.
194
195 If the response does not contain an "Expires" or a "Cache-Control"
196 header, then this function will apply some simple heuristic based
197 on the "Last-Modified" header to determine a suitable lifetime.
198 The following options might be passed to control the heuristics:
199
200 heuristic_expiry => $bool
201 If passed as a FALSE value, don't apply heuristics and just
202 return "undef" when "Expires" or "Cache-Control" is lacking.
203
204 h_lastmod_fraction => $num
205 This number represent the fraction of the difference since the
206 "Last-Modified" timestamp to make the expiry time. The default
207 is 0.10, the suggested typical setting of 10% in RFC 2616.
208
209 h_min => $sec
210 This is the lower limit of the heuristic expiry age to use.
211 The default is 60 (1 minute).
212
213 h_max => $sec
214 This is the upper limit of the heuristic expiry age to use.
215 The default is 86400 (24 hours).
216
217 h_default => $sec
218 This is the expiry age to use when nothing else applies. The
219 default is 3600 (1 hour) or "h_min" if greater.
220
221 $r->is_fresh( %opt )
222 Returns TRUE if the response is fresh, based on the values of
223 freshness_lifetime() and current_age(). If the response is no
224 longer fresh, then it has to be re-fetched or re-validated by the
225 origin server.
226
227 Options might be passed to control expiry heuristics, see the
228 description of freshness_lifetime().
229
230 $r->fresh_until( %opt )
231 Returns the time (seconds since epoch) when this entity is no
232 longer fresh.
233
234 Options might be passed to control expiry heuristics, see the
235 description of freshness_lifetime().
236
238 HTTP::Headers, HTTP::Message, HTTP::Status, HTTP::Request
239
241 Gisle Aas <gisle@activestate.com>
242
244 This software is copyright (c) 1994-2017 by Gisle Aas.
245
246 This is free software; you can redistribute it and/or modify it under
247 the same terms as the Perl 5 programming language system itself.
248
249
250
251perl v5.28.0 2018-06-05 HTTP::Response(3)