2
# $Id: Request.pm,v 1.30 2001/11/15 06:42:40 gisle Exp $
4
1
package HTTP::Request;
8
HTTP::Request - Class encapsulating HTTP Requests
12
require HTTP::Request;
13
$request = HTTP::Request->new(GET => 'http://www.oslo.net/');
17
C<HTTP::Request> is a class encapsulating HTTP style requests,
18
consisting of a request line, some headers, and some (potentially empty)
19
content. Note that the LWP library also uses this HTTP style requests
20
for non-HTTP protocols.
22
Instances of this class are usually passed to the C<request()> method
23
of an C<LWP::UserAgent> object:
25
$ua = LWP::UserAgent->new;
26
$request = HTTP::Request->new(GET => 'http://www.oslo.net/');
27
$response = $ua->request($request);
29
C<HTTP::Request> is a subclass of C<HTTP::Message> and therefore
30
inherits its methods. The inherited methods most often used are header(),
31
push_header(), remove_header(), and content(). See L<HTTP::Message> for details.
33
The following additional methods are available:
3
# $Id: Request.pm,v 1.40 2004/04/07 10:44:47 gisle Exp $
39
5
require HTTP::Message;
40
6
@ISA = qw(HTTP::Message);
41
$VERSION = sprintf("%d.%02d", q$Revision: 1.30 $ =~ /(\d+)\.(\d+)/);
7
$VERSION = sprintf("%d.%02d", q$Revision: 1.40 $ =~ /(\d+)\.(\d+)/);
45
=item $r = HTTP::Request->new($method, $uri)
47
=item $r = HTTP::Request->new($method, $uri, $header)
49
=item $r = HTTP::Request->new($method, $uri, $header, $content)
51
Constructs a new C<HTTP::Request> object describing a request on the
52
object C<$uri> using method C<$method>. The C<$uri> argument can be
53
either a string, or a reference to a C<URI> object. The optional $header
54
argument should be a reference to an C<HTTP::Headers> object.
55
The optional $content argument should be a string.
79
=item $r->method([$val])
83
These methods provide public access to the attributes containing
84
respectively the method of the request and the URI object of the
87
If an argument is given the attribute is given that as its new
88
value. If no argument is given the value is not touched. In either
89
case the previous value is returned.
91
The method() method argument should be a string.
93
The uri() method accept both a reference to a URI object and a
94
string as its argument. If a string is given, then it should be
95
parseable as an absolute URI.
99
sub method { shift->_elem('_method', @_); }
56
shift->_elem('_method', @_);
128
*url = \&uri; # this is the same for now
130
=item $r->as_string()
132
Method returning a textual representation of the request.
133
Mainly useful for debugging purposes. It takes no arguments.
89
*url = \&uri; # legacy
141
#push(@result, "---- $self -----");
142
my $req_line = $self->method || "[NO METHOD]";
96
$eol = "\n" unless defined $eol;
98
my $req_line = $self->method || "-";
143
99
my $uri = $self->uri;
144
$uri = (defined $uri) ? $uri->as_string : "[NO URI]";
100
$uri = (defined $uri) ? $uri->as_string : "-";
145
101
$req_line .= " $uri";
146
102
my $proto = $self->protocol;
147
103
$req_line .= " $proto" if $proto;
149
push(@result, $req_line);
150
push(@result, $self->headers_as_string);
151
my $content = $self->content;
152
if (defined $content) {
153
push(@result, $content);
155
#push(@result, ("-" x 40));
156
join("\n", @result, "");
105
return join($eol, $req_line, $self->SUPER::as_string(@_));
115
HTTP::Request - HTTP style request message
119
require HTTP::Request;
120
$request = HTTP::Request->new(GET => 'http://www.example.com/');
122
and usually used like this:
124
$ua = LWP::UserAgent->new;
125
$response = $ua->request($request);
129
C<HTTP::Request> is a class encapsulating HTTP style requests,
130
consisting of a request line, some headers, and a content body. Note
131
that the LWP library uses HTTP style requests even for non-HTTP
132
protocols. Instances of this class are usually passed to the
133
request() method of an C<LWP::UserAgent> object.
135
C<HTTP::Request> is a subclass of C<HTTP::Message> and therefore
136
inherits its methods. The following additional methods are available:
140
=item $r = HTTP::Request->new( $method, $uri )
142
=item $r = HTTP::Request->new( $method, $uri, $header )
144
=item $r = HTTP::Request->new( $method, $uri, $header, $content )
146
Constructs a new C<HTTP::Request> object describing a request on the
147
object $uri using method $method. The $method argument must be a
148
string. The $uri argument can be either a string, or a reference to a
149
C<URI> object. The optional $header argument should be a reference to
150
an C<HTTP::Headers> object or a plain array reference of key/value
151
pairs. The optional $content argument should be a string of bytes.
153
=item $r = HTTP::Request->parse( $str )
155
This constructs a new request object by parsing the given string.
159
=item $r->method( $val )
161
This is used to get/set the method attribute. The method should be a
162
short string like "GET", "HEAD", "PUT" or "POST".
166
=item $r->uri( $val )
168
This is used to get/set the uri attribute. The $val can be a
169
reference to a URI object or a plain string. If a string is given,
170
then it should be parseable as an absolute URI.
172
=item $r->header( $field )
174
=item $r->header( $field => $value )
176
This is used to get/set header values and it is inherited from
177
C<HTTP::Headers> via C<HTTP::Message>. See L<HTTP::Headers> for
178
details and other similar methods that can be used to access the
183
=item $r->content( $content )
185
This is used to get/set the content and it is inherited from the
186
C<HTTP::Message> base class. See L<HTTP::Message> for details and
187
other methods that can be used to access the content.
189
Note that the content should be a string of bytes. Strings in perl
190
can contain characters outside the range of a byte. The C<Encode>
191
module can be used to turn such strings into a string of bytes.
195
=item $r->as_string( $eol )
197
Method returning a textual representation of the request.
165
L<HTTP::Headers>, L<HTTP::Message>, L<HTTP::Request::Common>
203
L<HTTP::Headers>, L<HTTP::Message>, L<HTTP::Request::Common>,
169
Copyright 1995-2001 Gisle Aas.
208
Copyright 1995-2004 Gisle Aas.
171
210
This library is free software; you can redistribute it and/or
172
211
modify it under the same terms as Perl itself.