@@ -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 => 'example@appsrocks.com',
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
231316L<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-
0 commit comments