Skip to content

Commit f39aa20

Browse files
committed
Merge branch 'pr/84'
2 parents d73140e + 900f27a commit f39aa20

File tree

1 file changed

+60
-41
lines changed

1 file changed

+60
-41
lines changed

lib/HTTP/Request/Common.pm

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -310,21 +310,27 @@ __END__
310310
$ua = LWP::UserAgent->new;
311311
$ua->request(GET 'http://www.sn.no/');
312312
$ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]);
313+
$ua->request(PATCH 'http://somewhere/foo', [foo => bar, bar => foo]);
314+
$ua->request(PUT 'http://somewhere/foo', [foo => bar, bar => foo]);
313315
314316
=head1 DESCRIPTION
315317
316-
This module provide functions that return newly created C<HTTP::Request>
318+
This module provides functions that return newly created C<HTTP::Request>
317319
objects. These functions are usually more convenient to use than the
318-
standard C<HTTP::Request> constructor for the most common requests. The
319-
following functions are provided:
320+
standard C<HTTP::Request> constructor for the most common requests.
321+
322+
Note that L<LWP::UserAgent> has several convenience methods, including
323+
C<get>, C<head>, C<delete>, C<post> and C<put>.
324+
325+
The following functions are provided:
320326
321327
=over 4
322328
323329
=item GET $url
324330
325331
=item GET $url, Header => Value,...
326332
327-
The GET() function returns an C<HTTP::Request> object initialized with
333+
The C<GET> function returns an L<HTTP::Request> object initialized with
328334
the "GET" method and the specified URL. It is roughly equivalent to the
329335
following call
330336
@@ -336,49 +342,51 @@ following call
336342
but is less cluttered. What is different is that a header named
337343
C<Content> will initialize the content part of the request instead of
338344
setting a header field. Note that GET requests should normally not
339-
have a content, so this hack makes more sense for the PUT(), PATCH()
340-
and POST() functions described below.
345+
have a content, so this hack makes more sense for the C<PUT>, C<PATCH>
346+
and C<POST> functions described below.
341347
342-
The get(...) method of C<LWP::UserAgent> exists as a shortcut for
343-
$ua->request(GET ...).
348+
The C<get(...)> method of L<LWP::UserAgent> exists as a shortcut for
349+
C<< $ua->request(GET ...) >>.
344350
345351
=item HEAD $url
346352
347353
=item HEAD $url, Header => Value,...
348354
349355
Like GET() but the method in the request is "HEAD".
350356
351-
The head(...) method of "LWP::UserAgent" exists as a shortcut for
352-
$ua->request(HEAD ...).
353-
354-
=item PUT $url
355-
356-
=item PUT $url, Header => Value,...
357+
The C<head(...)> method of L<LWP::UserAgent> exists as a shortcut for
358+
C<< $ua->request(HEAD ...) >>.
357359
358-
=item PUT $url, Header => Value,..., Content => $content
360+
=item DELETE $url
359361
360-
Like GET() but the method in the request is "PUT".
362+
=item DELETE $url, Header => Value,...
361363
362-
The content of the request can be specified using the "Content"
363-
pseudo-header. This steals a bit of the header field namespace as
364-
there is no way to directly specify a header that is actually called
365-
"Content". If you really need this you must update the request
366-
returned in a separate statement.
364+
Like C<GET> but the method in the request is C<DELETE>. This function
365+
is not exported by default.
367366
368367
=item PATCH $url
369368
370369
=item PATCH $url, Header => Value,...
371370
371+
=item PATCH $url, $form_ref, Header => Value,...
372+
373+
=item PATCH $url, Header => Value,..., Content => $form_ref
374+
372375
=item PATCH $url, Header => Value,..., Content => $content
373376
374-
Like PUT() but the method in the request is "PATCH".
377+
The same as C<POST> below, but the method in the request is C<PATCH>.
375378
376-
=item DELETE $url
379+
=item PUT $url
377380
378-
=item DELETE $url, Header => Value,...
381+
=item PUT $url, Header => Value,...
379382
380-
Like GET() but the method in the request is "DELETE". This function
381-
is not exported by default.
383+
=item PUT $url, $form_ref, Header => Value,...
384+
385+
=item PUT $url, Header => Value,..., Content => $form_ref
386+
387+
=item PUT $url, Header => Value,..., Content => $content
388+
389+
The same as C<POST> below, but the method in the request is C<PUT>
382390
383391
=item POST $url
384392
@@ -390,13 +398,24 @@ is not exported by default.
390398
391399
=item POST $url, Header => Value,..., Content => $content
392400
393-
This works mostly like PUT() with "POST" as the method, but this
394-
function also takes a second optional array or hash reference
395-
parameter $form_ref. As for PUT() the content can also be specified
396-
directly using the "Content" pseudo-header, and you may also provide
397-
the $form_ref this way.
401+
C<POST>, C<PATCH> and C<PUT> all work with the same parameters.
402+
403+
%data = ( title => 'something', body => something else' );
404+
$ua = LWP::UserAgent->new();
405+
$request = HTTP::Request::Common::POST( $url, [ %data ] );
406+
$response = $ua->request($request);
407+
408+
They take a second optional array or hash reference
409+
parameter C<$form_ref>. The content can also be specified
410+
directly using the C<Content> pseudo-header, and you may also provide
411+
the C<$form_ref> this way.
412+
413+
The C<Content> pseudo-header steals a bit of the header field namespace as
414+
there is no way to directly specify a header that is actually called
415+
"Content". If you really need this you must update the request
416+
returned in a separate statement.
398417
399-
The $form_ref argument can be used to pass key/value pairs for the
418+
The C<$form_ref> argument can be used to pass key/value pairs for the
400419
form content. By default we will initialize a request using the
401420
C<application/x-www-form-urlencoded> content type. This means that
402421
you can emulate an HTML E<lt>form> POSTing like this:
@@ -409,7 +428,7 @@ you can emulate an HTML E<lt>form> POSTing like this:
409428
perc => '3%',
410429
];
411430
412-
This will create an HTTP::Request object that looks like this:
431+
This will create an L<HTTP::Request> object that looks like this:
413432
414433
POST http://www.perl.org/survey.cgi
415434
Content-Length: 66
@@ -423,7 +442,7 @@ name or by passing the value as an array reference.
423442
The POST method also supports the C<multipart/form-data> content used
424443
for I<Form-based File Upload> as specified in RFC 1867. You trigger
425444
this content format by specifying a content type of C<'form-data'> as
426-
one of the request headers. If one of the values in the $form_ref is
445+
one of the request headers. If one of the values in the C<$form_ref> is
427446
an array reference, then it is treated as a file part specification
428447
with the following interpretation:
429448
@@ -441,7 +460,7 @@ want to suppress sending the filename when you provide a $file value.
441460
442461
If a $file is provided by no C<Content-Type> header, then C<Content-Type>
443462
and C<Content-Encoding> will be filled in automatically with the values
444-
returned by LWP::MediaTypes::guess_media_type()
463+
returned by C<LWP::MediaTypes::guess_media_type()>
445464
446465
Sending my F<~/.profile> to the survey used as example above can be
447466
achieved by this:
@@ -455,7 +474,7 @@ achieved by this:
455474
init => ["$ENV{HOME}/.profile"],
456475
]
457476
458-
This will create an HTTP::Request object that almost looks this (the
477+
This will create an L<HTTP::Request> object that almost looks this (the
459478
boundary and the content of your F<~/.profile> is likely to be
460479
different):
461480
@@ -488,20 +507,20 @@ different):
488507
489508
--6G+f--
490509
491-
If you set the $DYNAMIC_FILE_UPLOAD variable (exportable) to some TRUE
510+
If you set the C<$DYNAMIC_FILE_UPLOAD> variable (exportable) to some TRUE
492511
value, then you get back a request object with a subroutine closure as
493512
the content attribute. This subroutine will read the content of any
494513
files on demand and return it in suitable chunks. This allow you to
495514
upload arbitrary big files without using lots of memory. You can even
496515
upload infinite files like F</dev/audio> if you wish; however, if
497-
the file is not a plain file, there will be no Content-Length header
516+
the file is not a plain file, there will be no C<Content-Length> header
498517
defined for the request. Not all servers (or server
499518
applications) like this. Also, if the file(s) change in size between
500-
the time the Content-Length is calculated and the time that the last
519+
the time the C<Content-Length> is calculated and the time that the last
501520
chunk is delivered, the subroutine will C<Croak>.
502521
503-
The post(...) method of "LWP::UserAgent" exists as a shortcut for
504-
$ua->request(POST ...).
522+
The C<post(...)> method of L<LWP::UserAgent> exists as a shortcut for
523+
C<< $ua->request(POST ...) >>.
505524
506525
=back
507526

0 commit comments

Comments
 (0)