Skip to content

Commit 7807b01

Browse files
authored
Merge pull request #92 from libwww-perl/batch-example
Add a few examples and show usage of ->add_part() for batch requests
2 parents 74b9567 + 7f648c5 commit 7807b01

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

Changes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Revision history for HTTP-Message
22

33
{{$NEXT}}
4+
- Add some useful examples in HTTP::Request.
5+
Batch requests are now explained.
46

57
6.13 2017-06-20 01:07:03Z
68
- Non-TRIAL release of changes found in 6.12

lib/HTTP/Request.pm

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,91 @@ Method returning a textual representation of the request.
226226
227227
=back
228228
229+
=head1 EXAMPLES
230+
231+
Creating requests to be sent with L<LWP::UserAgent> or others can be easy. Here
232+
are a few examples.
233+
234+
=head2 Simple POST
235+
236+
Here, we'll create a simple POST request that could be used to send JSON data
237+
to an endpoint.
238+
239+
#!/usr/bin/env perl
240+
241+
use strict;
242+
use warnings;
243+
244+
use Encode qw(encode_utf8);
245+
use HTTP::Request ();
246+
use JSON::MaybeXS qw(encode_json);
247+
248+
my $url = 'https://www.example.com/api/user/123';
249+
my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
250+
my $data = {foo => 'bar', baz => 'quux'};
251+
my $encoded_data = encode_utf8(encode_json($data));
252+
253+
my $r = HTTP::Request->new('POST', $url, $header, $encoded_data);
254+
# at this point, we could send it via LWP::UserAgent
255+
# my $ua = LWP::UserAgent->new();
256+
# my $res = $ua->request($r);
257+
258+
=head2 Batch POST Request
259+
260+
Some services, like Google, allow multiple requests to be sent in one batch.
261+
L<https://developers.google.com/drive/v3/web/batch> for example. Using the
262+
C<add_part> method from L<HTTP::Message> makes this simple.
263+
264+
#!/usr/bin/env perl
265+
266+
use strict;
267+
use warnings;
268+
269+
use Encode qw(encode_utf8);
270+
use HTTP::Request ();
271+
use JSON::MaybeXS qw(encode_json);
272+
273+
my $auth_token = 'auth_token';
274+
my $batch_url = 'https://www.googleapis.com/batch';
275+
my $url = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id';
276+
my $url_no_email = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id&sendNotificationEmail=false';
277+
278+
# generate a JSON post request for one of the batch entries
279+
my $req1 = build_json_request($url, {
280+
emailAddress => '[email protected]',
281+
role => "writer",
282+
type => "user",
283+
});
284+
285+
# generate a JSON post request for one of the batch entries
286+
my $req2 = build_json_request($url_no_email, {
287+
domain => "appsrocks.com",
288+
role => "reader",
289+
type => "domain",
290+
});
291+
292+
# generate a multipart request to send all of the other requests
293+
my $r = HTTP::Request->new('POST', $batch_url, [
294+
'Accept-Encoding' => 'gzip',
295+
# if we don't provide a boundary here, HTTP::Message will generate
296+
# one for us. We could use UUID::uuid() here if we wanted.
297+
'Content-Type' => 'multipart/mixed; boundary=END_OF_PART'
298+
]);
299+
300+
# add the two POST requests to the main request
301+
$r->add_part($req1, $req2);
302+
# at this point, we could send it via LWP::UserAgent
303+
# my $ua = LWP::UserAgent->new();
304+
# my $res = $ua->request($r);
305+
exit();
306+
307+
sub build_json_request {
308+
my ($url, $href) = @_;
309+
my $header = ['Authorization' => "Bearer $auth_token", 'Content-Type' => 'application/json; charset=UTF-8'];
310+
return HTTP::Request->new('POST', $url, $header, encode_utf8(encode_json($href)));
311+
}
312+
313+
229314
=head1 SEE ALSO
230315
231316
L<HTTP::Headers>, L<HTTP::Message>, L<HTTP::Request::Common>,
@@ -234,4 +319,3 @@ L<HTTP::Response>
234319
=cut
235320
236321
#ABSTRACT: HTTP style request message
237-

lib/HTTP/Request/Common.pm

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ sub form_data # RFC1867
215215
# or perhaps a file in the /proc file system where
216216
# stat may return a 0 size even though reading it
217217
# will produce data. So we cannot make
218-
# a Content-Length header.
218+
# a Content-Length header.
219219
undef $length;
220220
last;
221221
}
@@ -259,7 +259,7 @@ sub form_data # RFC1867
259259
}
260260
if ($buflength) {
261261
defined $length && ($length -= $buflength);
262-
return $buf
262+
return $buf
263263
}
264264
}
265265
};
@@ -317,9 +317,9 @@ __END__
317317
318318
This module provides functions that return newly created C<HTTP::Request>
319319
objects. These functions are usually more convenient to use than the
320-
standard C<HTTP::Request> constructor for the most common requests.
320+
standard C<HTTP::Request> constructor for the most common requests.
321321
322-
Note that L<LWP::UserAgent> has several convenience methods, including
322+
Note that L<LWP::UserAgent> has several convenience methods, including
323323
C<get>, C<head>, C<delete>, C<post> and C<put>.
324324
325325
The following functions are provided:
@@ -528,7 +528,9 @@ C<< $ua->request(POST ...) >>.
528528
529529
L<HTTP::Request>, L<LWP::UserAgent>
530530
531+
Also, there are some examples in L<HTTP::Request/"EXAMPLES"> that you might
532+
find useful. For example, batch requests are explained there.
533+
531534
=cut
532535
533536
#ABSTRACT: Construct common HTTP::Request objects
534-

0 commit comments

Comments
 (0)