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

SEE ALSO

159       HTTP::Headers, HTTP::Message, HTTP::Request::Common, HTTP::Response
160

AUTHOR

162       Gisle Aas <gisle@activestate.com>
163
165       This software is copyright (c) 1994 by Gisle Aas.
166
167       This is free software; you can redistribute it and/or modify it under
168       the same terms as the Perl 5 programming language system itself.
169
170
171
172perl v5.32.0                      2020-09-10                  HTTP::Request(3)
Impressum