286
298
=head1 DESCRIPTION
288
This module provide functions that return newly created HTTP::Request
300
This module provide functions that return newly created C<HTTP::Request>
289
301
objects. These functions are usually more convenient to use than the
290
standard HTTP::Request constructor for these common requests. The
291
following functions are provided.
302
standard C<HTTP::Request> constructor for the most common requests. The
303
following functions are provided:
295
309
=item GET $url, Header => Value,...
297
The GET() function returns a HTTP::Request object initialized with the
298
GET method and the specified URL. Without additional arguments it
299
is exactly equivalent to the following call
301
HTTP::Request->new(GET => $url)
303
but is less cluttered. It also reads better when used together with the
304
LWP::UserAgent->request() method:
306
my $ua = new LWP::UserAgent;
307
my $res = $ua->request(GET 'http://www.sn.no')
308
if ($res->is_success) { ...
310
You can also initialize header values in the request by specifying
311
some key/value pairs as optional arguments. For instance:
313
$ua->request(GET 'http://www.sn.no',
315
From => 'gisle@aas.no',
318
A header key called 'Content' is special and when seen the value will
319
initialize the content part of the request instead of setting a header.
321
=item HEAD $url, [Header => Value,...]
323
Like GET() but the method in the request is HEAD.
325
=item PUT $url, [Header => Value,...]
327
Like GET() but the method in the request is PUT.
329
=item POST $url, [$form_ref], [Header => Value,...]
331
This works mostly like GET() with POST as the method, but this function
311
The GET() function returns an C<HTTP::Request> object initialized with
312
the "GET" method and the specified URL. It is roughly equivalent to the
317
HTTP::Headers->new(Header => Value,...),
320
but is less cluttered. What is different is that a header named
321
C<Content> will initialize the content part of the request instead of
322
setting a header field. Note that GET requests should normally not
323
have a content, so this hack makes more sense for the PUT() and POST()
324
functions described below.
326
The get(...) method of C<LWP::UserAgent> exists as a shortcut for
327
$ua->request(GET ...).
331
=item HEAD $url, Header => Value,...
333
Like GET() but the method in the request is "HEAD".
335
The head(...) method of "LWP::UserAgent" exists as a shortcut for
336
$ua->request(HEAD ...).
340
=item PUT $url, Header => Value,...
342
=item PUT $url, Header => Value,..., Content => $content
344
Like GET() but the method in the request is "PUT".
348
=item POST $url, Header => Value,...
350
=item POST $url, $form_ref, Header => Value,...
352
=item POST $url, Header => Value,..., Content => $form_ref
354
This works mostly like GET() with "POST" as the method, but this function
332
355
also takes a second optional array or hash reference parameter
333
356
($form_ref). This argument can be used to pass key/value pairs for
334
357
the form content. By default we will initialize a request using the
359
385
with the following interpretation:
361
387
[ $file, $filename, Header => Value... ]
388
[ undef, $filename, Header => Value,..., Content => $content ]
363
390
The first value in the array ($file) is the name of a file to open.
364
391
This file will be read and its content placed in the request. The
365
routine will croak if the file can't be opened. Use an C<undef> as $file
366
value if you want to specify the content directly. The $filename is
367
the filename to report in the request. If this value is undefined,
368
then the basename of the $file will be used. You can specify an empty
369
string as $filename if you don't want any filename in the request.
392
routine will croak if the file can't be opened. Use an C<undef> as
393
$file value if you want to specify the content directly with a
394
C<Content> header. The $filename is the filename to report in the
395
request. If this value is undefined, then the basename of the $file
396
will be used. You can specify an empty string as $filename if you
397
want to suppress sending the filename when you provide a $file value.
399
If a $file is provided by no C<Content-Type> header, then C<Content-Type>
400
and C<Content-Encoding> will be filled in automatically with the values
401
returned by LWP::MediaTypes::guess_media_type()
371
403
Sending my F<~/.profile> to the survey used as example above can be
372
404
achieved by this:
419
451
files on demand and return it in suitable chunks. This allow you to
420
452
upload arbitrary big files without using lots of memory. You can even
421
453
upload infinite files like F</dev/audio> if you wish; however, if
422
the file is not a plain file, there will be no Content-Length header
454
the file is not a plain file, there will be no Content-Length header
423
455
defined for the request. Not all servers (or server
424
456
applications) like this. Also, if the file(s) change in size between
425
457
the time the Content-Length is calculated and the time that the last
426
458
chunk is delivered, the subroutine will C<Croak>.
460
The post(...) method of "LWP::UserAgent" exists as a shortcut for
461
$ua->request(POST ...).