1HTTP::Request::Common(3U)ser Contributed Perl DocumentatiHoTnTP::Request::Common(3)
2
3
4
6 HTTP::Request::Common - Construct common HTTP::Request objects
7
9 use HTTP::Request::Common;
10 $ua = LWP::UserAgent->new;
11 $ua->request(GET 'http://www.sn.no/');
12 $ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]);
13
15 This module provide functions that return newly created "HTTP::Request"
16 objects. These functions are usually more convenient to use than the
17 standard "HTTP::Request" constructor for the most common requests. The
18 following functions are provided:
19
20 GET $url
21 GET $url, Header => Value,...
22 The GET() function returns an "HTTP::Request" object initialized
23 with the "GET" method and the specified URL. It is roughly
24 equivalent to the following call
25
26 HTTP::Request->new(
27 GET => $url,
28 HTTP::Headers->new(Header => Value,...),
29 )
30
31 but is less cluttered. What is different is that a header named
32 "Content" will initialize the content part of the request instead
33 of setting a header field. Note that GET requests should normally
34 not have a content, so this hack makes more sense for the PUT() and
35 POST() functions described below.
36
37 The get(...) method of "LWP::UserAgent" exists as a shortcut for
38 $ua->request(GET ...).
39
40 HEAD $url
41 HEAD $url, Header => Value,...
42 Like GET() but the method in the request is "HEAD".
43
44 The head(...) method of "LWP::UserAgent" exists as a shortcut for
45 $ua->request(HEAD ...).
46
47 PUT $url
48 PUT $url, Header => Value,...
49 PUT $url, Header => Value,..., Content => $content
50 Like GET() but the method in the request is "PUT".
51
52 The content of the request can be specified using the "Content"
53 pseudo-header. This steals a bit of the header field namespace as
54 there is no way to directly specify a header that is actually
55 called "Content". If you really need this you must update the
56 request returned in a separate statement.
57
58 DELETE $url
59 DELETE $url, Header => Value,...
60 Like GET() but the method in the request is "DELETE". This
61 function is not exported by default.
62
63 POST $url
64 POST $url, Header => Value,...
65 POST $url, $form_ref, Header => Value,...
66 POST $url, Header => Value,..., Content => $form_ref
67 POST $url, Header => Value,..., Content => $content
68 This works mostly like PUT() with "POST" as the method, but this
69 function also takes a second optional array or hash reference
70 parameter $form_ref. As for PUT() the content can also be
71 specified directly using the "Content" pseudo-header, and you may
72 also provide the $form_ref this way.
73
74 The $form_ref argument can be used to pass key/value pairs for the
75 form content. By default we will initialize a request using the
76 "application/x-www-form-urlencoded" content type. This means that
77 you can emulate a HTML <form> POSTing like this:
78
79 POST 'http://www.perl.org/survey.cgi',
80 [ name => 'Gisle Aas',
81 email => 'gisle@aas.no',
82 gender => 'M',
83 born => '1964',
84 perc => '3%',
85 ];
86
87 This will create a HTTP::Request object that looks like this:
88
89 POST http://www.perl.org/survey.cgi
90 Content-Length: 66
91 Content-Type: application/x-www-form-urlencoded
92
93 name=Gisle%20Aas&email=gisle%40aas.no&gender=M&born=1964&perc=3%25
94
95 Multivalued form fields can be specified by either repeating the
96 field name or by passing the value as an array reference.
97
98 The POST method also supports the "multipart/form-data" content
99 used for Form-based File Upload as specified in RFC 1867. You
100 trigger this content format by specifying a content type of
101 'form-data' as one of the request headers. If one of the values in
102 the $form_ref is an array reference, then it is treated as a file
103 part specification with the following interpretation:
104
105 [ $file, $filename, Header => Value... ]
106 [ undef, $filename, Header => Value,..., Content => $content ]
107
108 The first value in the array ($file) is the name of a file to open.
109 This file will be read and its content placed in the request. The
110 routine will croak if the file can't be opened. Use an "undef" as
111 $file value if you want to specify the content directly with a
112 "Content" header. The $filename is the filename to report in the
113 request. If this value is undefined, then the basename of the
114 $file will be used. You can specify an empty string as $filename
115 if you want to suppress sending the filename when you provide a
116 $file value.
117
118 If a $file is provided by no "Content-Type" header, then
119 "Content-Type" and "Content-Encoding" will be filled in
120 automatically with the values returned by
121 LWP::MediaTypes::guess_media_type()
122
123 Sending my ~/.profile to the survey used as example above can be
124 achieved by this:
125
126 POST 'http://www.perl.org/survey.cgi',
127 Content_Type => 'form-data',
128 Content => [ name => 'Gisle Aas',
129 email => 'gisle@aas.no',
130 gender => 'M',
131 born => '1964',
132 init => ["$ENV{HOME}/.profile"],
133 ]
134
135 This will create a HTTP::Request object that almost looks this (the
136 boundary and the content of your ~/.profile is likely to be
137 different):
138
139 POST http://www.perl.org/survey.cgi
140 Content-Length: 388
141 Content-Type: multipart/form-data; boundary="6G+f"
142
143 --6G+f
144 Content-Disposition: form-data; name="name"
145
146 Gisle Aas
147 --6G+f
148 Content-Disposition: form-data; name="email"
149
150 gisle@aas.no
151 --6G+f
152 Content-Disposition: form-data; name="gender"
153
154 M
155 --6G+f
156 Content-Disposition: form-data; name="born"
157
158 1964
159 --6G+f
160 Content-Disposition: form-data; name="init"; filename=".profile"
161 Content-Type: text/plain
162
163 PATH=/local/perl/bin:$PATH
164 export PATH
165
166 --6G+f--
167
168 If you set the $DYNAMIC_FILE_UPLOAD variable (exportable) to some
169 TRUE value, then you get back a request object with a subroutine
170 closure as the content attribute. This subroutine will read the
171 content of any files on demand and return it in suitable chunks.
172 This allow you to upload arbitrary big files without using lots of
173 memory. You can even upload infinite files like /dev/audio if you
174 wish; however, if the file is not a plain file, there will be no
175 Content-Length header defined for the request. Not all servers (or
176 server applications) like this. Also, if the file(s) change in
177 size between the time the Content-Length is calculated and the time
178 that the last chunk is delivered, the subroutine will "Croak".
179
180 The post(...) method of "LWP::UserAgent" exists as a shortcut for
181 $ua->request(POST ...).
182
184 HTTP::Request, LWP::UserAgent
185
187 Copyright 1997-2004, Gisle Aas
188
189 This library is free software; you can redistribute it and/or modify it
190 under the same terms as Perl itself.
191
192
193
194perl v5.10.1 2009-06-15 HTTP::Request::Common(3)