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->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 inher‐
29 its 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 $con‐
39 tent argument should be a string of bytes. The meaning these argu‐
40 ments 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 a 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( $content )
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 has 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 unautho‐
87 rized. The value is "undef" if this is the first response in a
88 chain.
89
90 $r->status_line
91 Returns the string "<code> <message>". If the message attribute is
92 not set then the official name of <code> (see HTTP::Status) is sub‐
93 stituted.
94
95 $r->base
96 Returns the base URI for this response. The return value will be a
97 reference to a URI object.
98
99 The base URI is obtained from one the following sources (in prior‐
100 ity order):
101
102 1. Embedded in the document content, for instance <BASE
103 HREF="..."> in HTML documents.
104
105 2. A "Content-Base:" or a "Content-Location:" header in the
106 response.
107
108 For backwards compatibility with older HTTP implementations we
109 will also look for the "Base:" header.
110
111 3. The URI used to request this response. This might not be the
112 original URI that was passed to $ua->request() method, because
113 we might have received some redirect responses first.
114
115 If neither of these sources provide an absolute URI, undef is
116 returned.
117
118 When the LWP protocol modules produce the HTTP::Response object,
119 then any base URI embedded in the document (step 1) will already
120 have initialized the "Content-Base:" header. This means that this
121 method only performs the last 2 steps (the content is not always
122 available either).
123
124 $r->as_string
125 $r->as_string( $eol )
126 Returns a textual representation of the response.
127
128 $r->is_info
129 $r->is_success
130 $r->is_redirect
131 $r->is_error
132 These methods indicate if the response was informational, success‐
133 ful, a redirection, or an error. See HTTP::Status for the meaning
134 of these.
135
136 $r->error_as_HTML
137 Returns a string containing a complete HTML document indicating
138 what error occurred. This method should only be called when
139 $r->is_error is TRUE.
140
141 $r->current_age
142 Calculates the "current age" of the response as specified by RFC
143 2616 section 13.2.3. The age of a response is the time since it
144 was sent by the origin server. The returned value is a number rep‐
145 resenting the age in seconds.
146
147 $r->freshness_lifetime
148 Calculates the "freshness lifetime" of the response as specified by
149 RFC 2616 section 13.2.4. The "freshness lifetime" is the length of
150 time between the generation of a response and its expiration time.
151 The returned value is a number representing the freshness lifetime
152 in seconds.
153
154 If the response does not contain an "Expires" or a "Cache-Control"
155 header, then this function will apply some simple heuristic based
156 on 'Last-Modified' to determine a suitable lifetime.
157
158 $r->is_fresh
159 Returns TRUE if the response is fresh, based on the values of
160 freshness_lifetime() and current_age(). If the response is no
161 longer fresh, then it has to be refetched or revalidated by the
162 origin server.
163
164 $r->fresh_until
165 Returns the time when this entity is no longer fresh.
166
168 HTTP::Headers, HTTP::Message, HTTP::Status, HTTP::Request
169
171 Copyright 1995-2004 Gisle Aas.
172
173 This library is free software; you can redistribute it and/or modify it
174 under the same terms as Perl itself.
175
176
177
178perl v5.8.8 2004-04-06 HTTP::Response(3)