1HTTP::Request(3)      User Contributed Perl Documentation     HTTP::Request(3)
2
3
4

NAME

6       HTTP::Request - HTTP style request message
7

VERSION

9       version 6.18
10

SYNOPSIS

12        require HTTP::Request;
13        $request = HTTP::Request->new(GET => 'http://www.example.com/');
14
15       and usually used like this:
16
17        $ua = LWP::UserAgent->new;
18        $response = $ua->request($request);
19

DESCRIPTION

21       "HTTP::Request" is a class encapsulating HTTP style requests,
22       consisting of a request line, some headers, and a content body. Note
23       that the LWP library uses HTTP style requests even for non-HTTP
24       protocols.  Instances of this class are usually passed to the request()
25       method of an "LWP::UserAgent" object.
26
27       "HTTP::Request" is a subclass of "HTTP::Message" and therefore inherits
28       its methods.  The following additional methods are available:
29
30       $r = HTTP::Request->new( $method, $uri )
31       $r = HTTP::Request->new( $method, $uri, $header )
32       $r = HTTP::Request->new( $method, $uri, $header, $content )
33           Constructs a new "HTTP::Request" object describing a request on the
34           object $uri using method $method.  The $method argument must be a
35           string.  The $uri argument can be either a string, or a reference
36           to a "URI" object.  The optional $header argument should be a
37           reference to an "HTTP::Headers" object or a plain array reference
38           of key/value pairs.  The optional $content argument should be a
39           string of bytes.
40
41       $r = HTTP::Request->parse( $str )
42           This constructs a new request object by parsing the given string.
43
44       $r->method
45       $r->method( $val )
46           This is used to get/set the method attribute.  The method should be
47           a short string like "GET", "HEAD", "PUT", "PATCH" or "POST".
48
49       $r->uri
50       $r->uri( $val )
51           This is used to get/set the uri attribute.  The $val can be a
52           reference to a URI object or a plain string.  If a string is given,
53           then it should be parsable as an absolute URI.
54
55       $r->header( $field )
56       $r->header( $field => $value )
57           This is used to get/set header values and it is inherited from
58           "HTTP::Headers" via "HTTP::Message".  See HTTP::Headers for details
59           and other similar methods that can be used to access the headers.
60
61       $r->accept_decodable
62           This will set the "Accept-Encoding" header to the list of encodings
63           that decoded_content() can decode.
64
65       $r->content
66       $r->content( $bytes )
67           This is used to get/set the content and it is inherited from the
68           "HTTP::Message" base class.  See HTTP::Message for details and
69           other methods that can be used to access the content.
70
71           Note that the content should be a string of bytes.  Strings in perl
72           can contain characters outside the range of a byte.  The "Encode"
73           module can be used to turn such strings into a string of bytes.
74
75       $r->as_string
76       $r->as_string( $eol )
77           Method returning a textual representation of the request.
78

EXAMPLES

80       Creating requests to be sent with LWP::UserAgent or others can be easy.
81       Here are a few examples.
82
83   Simple POST
84       Here, we'll create a simple POST request that could be used to send
85       JSON data to an endpoint.
86
87           #!/usr/bin/env perl
88
89           use strict;
90           use warnings;
91
92           use Encode qw(encode_utf8);
93           use HTTP::Request ();
94           use JSON::MaybeXS qw(encode_json);
95
96           my $url = 'https://www.example.com/api/user/123';
97           my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
98           my $data = {foo => 'bar', baz => 'quux'};
99           my $encoded_data = encode_utf8(encode_json($data));
100
101           my $r = HTTP::Request->new('POST', $url, $header, $encoded_data);
102           # at this point, we could send it via LWP::UserAgent
103           # my $ua = LWP::UserAgent->new();
104           # my $res = $ua->request($r);
105
106   Batch POST Request
107       Some services, like Google, allow multiple requests to be sent in one
108       batch.  <https://developers.google.com/drive/v3/web/batch> for example.
109       Using the "add_part" method from HTTP::Message makes this simple.
110
111           #!/usr/bin/env perl
112
113           use strict;
114           use warnings;
115
116           use Encode qw(encode_utf8);
117           use HTTP::Request ();
118           use JSON::MaybeXS qw(encode_json);
119
120           my $auth_token = 'auth_token';
121           my $batch_url = 'https://www.googleapis.com/batch';
122           my $url = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id';
123           my $url_no_email = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id&sendNotificationEmail=false';
124
125           # generate a JSON post request for one of the batch entries
126           my $req1 = build_json_request($url, {
127               emailAddress => 'example@appsrocks.com',
128               role => "writer",
129               type => "user",
130           });
131
132           # generate a JSON post request for one of the batch entries
133           my $req2 = build_json_request($url_no_email, {
134               domain => "appsrocks.com",
135               role => "reader",
136               type => "domain",
137           });
138
139           # generate a multipart request to send all of the other requests
140           my $r = HTTP::Request->new('POST', $batch_url, [
141               'Accept-Encoding' => 'gzip',
142               # if we don't provide a boundary here, HTTP::Message will generate
143               # one for us. We could use UUID::uuid() here if we wanted.
144               'Content-Type' => 'multipart/mixed; boundary=END_OF_PART'
145           ]);
146
147           # add the two POST requests to the main request
148           $r->add_part($req1, $req2);
149           # at this point, we could send it via LWP::UserAgent
150           # my $ua = LWP::UserAgent->new();
151           # my $res = $ua->request($r);
152           exit();
153
154           sub build_json_request {
155               my ($url, $href) = @_;
156               my $header = ['Authorization' => "Bearer $auth_token", 'Content-Type' => 'application/json; charset=UTF-8'];
157               return HTTP::Request->new('POST', $url, $header, encode_utf8(encode_json($href)));
158           }
159

SEE ALSO

161       HTTP::Headers, HTTP::Message, HTTP::Request::Common, HTTP::Response
162

AUTHOR

164       Gisle Aas <gisle@activestate.com>
165
167       This software is copyright (c) 1994-2017 by Gisle Aas.
168
169       This is free software; you can redistribute it and/or modify it under
170       the same terms as the Perl 5 programming language system itself.
171
172
173
174perl v5.28.1                      2018-06-05                  HTTP::Request(3)
Impressum