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