{"id":7249,"date":"2022-12-20T19:35:59","date_gmt":"2022-12-20T22:35:59","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/httprequest-man3\/"},"modified":"2022-12-20T19:35:59","modified_gmt":"2022-12-20T22:35:59","slug":"httprequest-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/httprequest-man3\/","title":{"rendered":"HTTP::Request (man3)"},"content":{"rendered":"<h1 align=\"center\">HTTP::Request<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#VERSION\">VERSION<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#AUTHOR\">AUTHOR<\/a><br \/> <a href=\"#COPYRIGHT AND LICENSE\">COPYRIGHT AND LICENSE<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">HTTP::Request \u2212 HTTP style request message<\/p>\n<h2>VERSION <a name=\"VERSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">version 6.26<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">require HTTP::Request; <br \/> $request = HTTP::Request\u2212>new(GET => &#8216;http:\/\/www.example.com\/&#8217;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">and usually used like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">$ua = LWP::UserAgent\u2212>new; <br \/> $response = $ua\u2212>request($request);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">&#8220;HTTP::Request&#8221; is a class encapsulating <small>HTTP<\/small> style requests, consisting of a request line, some headers, and a content body. Note that the <small>LWP<\/small> library uses <small>HTTP<\/small> style requests even for non-HTTP protocols. Instances of this class are usually passed to the <b>request()<\/b> method of an &#8220;LWP::UserAgent&#8221; object.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">&#8220;HTTP::Request&#8221; is a subclass of &#8220;HTTP::Message&#8221; and therefore inherits its methods. The following additional methods are available: <br \/> $r = HTTP::Request\u2212>new( $method, $uri ) <br \/> $r = HTTP::Request\u2212>new( $method, $uri, $header ) <br \/> $r = HTTP::Request\u2212>new( $method, $uri, $header, $content )<\/p>\n<p style=\"margin-left:17%;\">Constructs a new &#8220;HTTP::Request&#8221; object describing a request on the object $uri using method $method. The $method argument must be a string. The $uri argument can be either a string, or a reference to a &#8220;URI&#8221; object. The optional $header argument should be a reference to an &#8220;HTTP::Headers&#8221; object or a plain array reference of key\/value pairs. The optional $content argument should be a string of bytes.<\/p>\n<p style=\"margin-left:11%;\">$r = HTTP::Request\u2212>parse( $str )<\/p>\n<p style=\"margin-left:17%;\">This constructs a new request object by parsing the given string.<\/p>\n<p style=\"margin-left:11%;\">$r\u2212>method <br \/> $r\u2212>method( $val )<\/p>\n<p style=\"margin-left:17%;\">This is used to get\/set the method attribute. The method should be a short string like &#8221; <small>GET&#8221;, &#8220;HEAD&#8221;, &#8220;PUT&#8221;, &#8220;PATCH&#8221;<\/small> or &#8221; <small>POST&#8221;.<\/small><\/p>\n<p style=\"margin-left:11%;\">$r\u2212>uri <br \/> $r\u2212>uri( $val )<\/p>\n<p style=\"margin-left:17%;\">This is used to get\/set the uri attribute. The $val can be a reference to a <small>URI<\/small> object or a plain string. If a string is given, then it should be parsable as an absolute <small>URI.<\/small><\/p>\n<p style=\"margin-left:11%;\">$r\u2212>header( $field ) <br \/> $r\u2212>header( $field => $value )<\/p>\n<p style=\"margin-left:17%;\">This is used to get\/set header values and it is inherited from &#8220;HTTP::Headers&#8221; via &#8220;HTTP::Message&#8221;. See HTTP::Headers for details and other similar methods that can be used to access the headers.<\/p>\n<p style=\"margin-left:11%;\">$r\u2212>accept_decodable<\/p>\n<p style=\"margin-left:17%;\">This will set the &#8220;Accept\u2212Encoding&#8221; header to the list of encodings that <b>decoded_content()<\/b> can decode.<\/p>\n<p style=\"margin-left:11%;\">$r\u2212>content <br \/> $r\u2212>content( $bytes )<\/p>\n<p style=\"margin-left:17%;\">This is used to get\/set the content and it is inherited from the &#8220;HTTP::Message&#8221; base class. See HTTP::Message for details and other methods that can be used to access the content.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">Note that the content should be a string of bytes. Strings in perl can contain characters outside the range of a byte. The &#8220;Encode&#8221; module can be used to turn such strings into a string of bytes.<\/p>\n<p style=\"margin-left:11%;\">$r\u2212>as_string <br \/> $r\u2212>as_string( $eol )<\/p>\n<p style=\"margin-left:17%;\">Method returning a textual representation of the request.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Creating requests to be sent with LWP::UserAgent or others can be easy. Here are a few examples.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Simple <small>POST<\/small><\/b> <br \/> Here, we\u2019ll create a simple <small>POST<\/small> request that could be used to send <small>JSON<\/small> data to an endpoint.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#!\/usr\/bin\/env perl <br \/> use strict; <br \/> use warnings; <br \/> use HTTP::Request (); <br \/> use JSON::MaybeXS qw(encode_json); <br \/> my $url = &#8216;https:\/\/www.example.com\/api\/user\/123&#8217;; <br \/> my $header = [&#8216;Content\u2212Type&#8217; => &#8216;application\/json; charset=UTF\u22128&#8217;]; <br \/> my $data = {foo => &#8216;bar&#8217;, baz => &#8216;quux&#8217;}; <br \/> my $encoded_data = encode_json($data); <br \/> my $r = HTTP::Request\u2212>new(&#8216;POST&#8217;, $url, $header, $encoded_data); <br \/> # at this point, we could send it via LWP::UserAgent <br \/> # my $ua = LWP::UserAgent\u2212>new(); <br \/> # my $res = $ua\u2212>request($r);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Batch <small>POST<\/small> Request<\/b> <br \/> Some services, like Google, allow multiple requests to be sent in one batch. <https:\/\/developers.google.com\/drive\/v3\/web\/batch> for example. Using the &#8220;add_part&#8221; method from HTTP::Message makes this simple.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#!\/usr\/bin\/env perl <br \/> use strict; <br \/> use warnings; <br \/> use HTTP::Request (); <br \/> use JSON::MaybeXS qw(encode_json); <br \/> my $auth_token = &#8216;auth_token&#8217;; <br \/> my $batch_url = &#8216;https:\/\/www.googleapis.com\/batch&#8217;; <br \/> my $url = &#8216;https:\/\/www.googleapis.com\/drive\/v3\/files\/fileId\/permissions?fields=id&#8217;; <br \/> my $url_no_email = &#8216;https:\/\/www.googleapis.com\/drive\/v3\/files\/fileId\/permissions?fields=id&#038;sendNotificationEmail=false&#8217;; <br \/> # generate a JSON post request for one of the batch entries <br \/> my $req1 = build_json_request($url, { <br \/> emailAddress => &#8216;example@appsrocks.com&#8217;, <br \/> role => &#8220;writer&#8221;, <br \/> type => &#8220;user&#8221;, <br \/> }); <br \/> # generate a JSON post request for one of the batch entries <br \/> my $req2 = build_json_request($url_no_email, { <br \/> domain => &#8220;appsrocks.com&#8221;, <br \/> role => &#8220;reader&#8221;, <br \/> type => &#8220;domain&#8221;, <br \/> }); <br \/> # generate a multipart request to send all of the other requests <br \/> my $r = HTTP::Request\u2212>new(&#8216;POST&#8217;, $batch_url, [ <br \/> &#8216;Accept\u2212Encoding&#8217; => &#8216;gzip&#8217;, <br \/> # if we don&#8217;t provide a boundary here, HTTP::Message will generate <br \/> # one for us. We could use UUID::uuid() here if we wanted. <br \/> &#8216;Content\u2212Type&#8217; => &#8216;multipart\/mixed; boundary=END_OF_PART&#8217; <br \/> ]); <br \/> # add the two POST requests to the main request <br \/> $r\u2212>add_part($req1, $req2); <br \/> # at this point, we could send it via LWP::UserAgent <br \/> # my $ua = LWP::UserAgent\u2212>new(); <br \/> # my $res = $ua\u2212>request($r); <br \/> exit(); <br \/> sub build_json_request { <br \/> my ($url, $href) = @_; <br \/> my $header = [&#8216;Authorization&#8217; => &#8220;Bearer $auth_token&#8221;, &#8216;Content\u2212Type&#8217; => &#8216;application\/json; charset=UTF\u22128&#8217;]; <br \/> return HTTP::Request\u2212>new(&#8216;POST&#8217;, $url, $header, encode_json($href)); <br \/> }<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">HTTP::Headers, HTTP::Message, HTTP::Request::Common, HTTP::Response<\/p>\n<h2>AUTHOR <a name=\"AUTHOR\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Gisle Aas <gisle@activestate.com><\/p>\n<h2>COPYRIGHT AND LICENSE <a name=\"COPYRIGHT AND LICENSE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This software is copyright (c) 1994 by Gisle Aas.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This is free software; you can redistribute it and\/or modify it under the same terms as the Perl 5 programming language system itself.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  HTTP::Request \u2212 HTTP style request message <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3575,3007],"class_list":["post-7249","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-httprequest","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7249"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7249\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}