{
    "mode": "perldoc",
    "parameter": "HTTP::Request",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/HTTP%3A%3ARequest/json",
    "generated": "2026-06-16T10:03:33Z",
    "synopsis": "require HTTP::Request;\n$request = HTTP::Request->new(GET => 'http://www.example.com/');\nand usually used like this:\n$ua = LWP::UserAgent->new;\n$response = $ua->request($request);",
    "sections": {
        "NAME": {
            "content": "HTTP::Request - HTTP style request message\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 6.36\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "require HTTP::Request;\n$request = HTTP::Request->new(GET => 'http://www.example.com/');\n\nand usually used like this:\n\n$ua = LWP::UserAgent->new;\n$response = $ua->request($request);\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "\"HTTP::Request\" is a class encapsulating HTTP style requests, consisting of a request line, some\nheaders, and a content body. Note that the LWP library uses HTTP style requests even for\nnon-HTTP protocols. Instances of this class are usually passed to the request() method of an\n\"LWP::UserAgent\" object.\n\n\"HTTP::Request\" is a subclass of \"HTTP::Message\" and therefore inherits its methods. The\nfollowing additional methods are available:\n\n$r = HTTP::Request->new( $method, $uri )\n$r = HTTP::Request->new( $method, $uri, $header )\n$r = HTTP::Request->new( $method, $uri, $header, $content )\nConstructs a new \"HTTP::Request\" object describing a request on the object $uri using method\n$method. The $method argument must be a string. The $uri argument can be either a string, or\na reference to a \"URI\" object. The optional $header argument should be a reference to an\n\"HTTP::Headers\" object or a plain array reference of key/value pairs. The optional $content\nargument should be a string of bytes.\n\n$r = HTTP::Request->parse( $str )\nThis constructs a new request object by parsing the given string.\n\n$r->method\n$r->method( $val )\nThis is used to get/set the method attribute. The method should be a short string like\n\"GET\", \"HEAD\", \"PUT\", \"PATCH\" or \"POST\".\n\n$r->uri\n$r->uri( $val )\nThis is used to get/set the uri attribute. The $val can be a reference to a URI object or a\nplain string. If a string is given, then it should be parsable as an absolute URI.\n\n$r->header( $field )\n$r->header( $field => $value )\nThis is used to get/set header values and it is inherited from \"HTTP::Headers\" via\n\"HTTP::Message\". See HTTP::Headers for details and other similar methods that can be used to\naccess the headers.\n\n$r->acceptdecodable\nThis will set the \"Accept-Encoding\" header to the list of encodings that decodedcontent()\ncan decode.\n\n$r->content\n$r->content( $bytes )\nThis is used to get/set the content and it is inherited from the \"HTTP::Message\" base class.\nSee HTTP::Message for details and other methods that can be used to access the content.\n\nNote that the content should be a string of bytes. Strings in perl can contain characters\noutside the range of a byte. The \"Encode\" module can be used to turn such strings into a\nstring of bytes.\n\n$r->asstring\n$r->asstring( $eol )\nMethod returning a textual representation of the request.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "Creating requests to be sent with LWP::UserAgent or others can be easy. Here are a few examples.\n",
            "subsections": [
                {
                    "name": "Simple POST",
                    "content": "Here, we'll create a simple POST request that could be used to send JSON data to an endpoint.\n\n#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\n\nuse HTTP::Request ();\nuse JSON::MaybeXS qw(encodejson);\n\nmy $url = 'https://www.example.com/api/user/123';\nmy $header = ['Content-Type' => 'application/json; charset=UTF-8'];\nmy $data = {foo => 'bar', baz => 'quux'};\nmy $encodeddata = encodejson($data);\n\nmy $r = HTTP::Request->new('POST', $url, $header, $encodeddata);\n# at this point, we could send it via LWP::UserAgent\n# my $ua = LWP::UserAgent->new();\n# my $res = $ua->request($r);\n"
                },
                {
                    "name": "Batch POST Request",
                    "content": "Some services, like Google, allow multiple requests to be sent in one batch.\n<https://developers.google.com/drive/v3/web/batch> for example. Using the \"addpart\" method from\nHTTP::Message makes this simple.\n\n#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\n\nuse HTTP::Request ();\nuse JSON::MaybeXS qw(encodejson);\n\nmy $authtoken = 'authtoken';\nmy $batchurl = 'https://www.googleapis.com/batch';\nmy $url = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id';\nmy $urlnoemail = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id&sendNotificationEmail=false';\n\n# generate a JSON post request for one of the batch entries\nmy $req1 = buildjsonrequest($url, {\nemailAddress => 'example@appsrocks.com',\nrole => \"writer\",\ntype => \"user\",\n});\n\n# generate a JSON post request for one of the batch entries\nmy $req2 = buildjsonrequest($urlnoemail, {\ndomain => \"appsrocks.com\",\nrole => \"reader\",\ntype => \"domain\",\n});\n\n# generate a multipart request to send all of the other requests\nmy $r = HTTP::Request->new('POST', $batchurl, [\n'Accept-Encoding' => 'gzip',\n# if we don't provide a boundary here, HTTP::Message will generate\n# one for us. We could use UUID::uuid() here if we wanted.\n'Content-Type' => 'multipart/mixed; boundary=ENDOFPART'\n]);\n\n# add the two POST requests to the main request\n$r->addpart($req1, $req2);\n# at this point, we could send it via LWP::UserAgent\n# my $ua = LWP::UserAgent->new();\n# my $res = $ua->request($r);\nexit();\n\nsub buildjsonrequest {\nmy ($url, $href) = @;\nmy $header = ['Authorization' => \"Bearer $authtoken\", 'Content-Type' => 'application/json; charset=UTF-8'];\nreturn HTTP::Request->new('POST', $url, $header, encodejson($href));\n}\n"
                }
            ]
        },
        "SEE ALSO": {
            "content": "HTTP::Headers, HTTP::Message, HTTP::Request::Common, HTTP::Response\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Gisle Aas <gisle@activestate.com>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 1994 by Gisle Aas.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        }
    },
    "summary": "HTTP::Request - HTTP style request message",
    "flags": [],
    "examples": [
        "Creating requests to be sent with LWP::UserAgent or others can be easy. Here are a few examples.",
        "Here, we'll create a simple POST request that could be used to send JSON data to an endpoint.",
        "#!/usr/bin/env perl",
        "use strict;",
        "use warnings;",
        "use HTTP::Request ();",
        "use JSON::MaybeXS qw(encodejson);",
        "my $url = 'https://www.example.com/api/user/123';",
        "my $header = ['Content-Type' => 'application/json; charset=UTF-8'];",
        "my $data = {foo => 'bar', baz => 'quux'};",
        "my $encodeddata = encodejson($data);",
        "my $r = HTTP::Request->new('POST', $url, $header, $encodeddata);",
        "# at this point, we could send it via LWP::UserAgent",
        "# my $ua = LWP::UserAgent->new();",
        "# my $res = $ua->request($r);",
        "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 \"addpart\" method from",
        "HTTP::Message makes this simple.",
        "#!/usr/bin/env perl",
        "use strict;",
        "use warnings;",
        "use HTTP::Request ();",
        "use JSON::MaybeXS qw(encodejson);",
        "my $authtoken = 'authtoken';",
        "my $batchurl = 'https://www.googleapis.com/batch';",
        "my $url = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id';",
        "my $urlnoemail = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id&sendNotificationEmail=false';",
        "# generate a JSON post request for one of the batch entries",
        "my $req1 = buildjsonrequest($url, {",
        "emailAddress => 'example@appsrocks.com',",
        "role => \"writer\",",
        "type => \"user\",",
        "});",
        "# generate a JSON post request for one of the batch entries",
        "my $req2 = buildjsonrequest($urlnoemail, {",
        "domain => \"appsrocks.com\",",
        "role => \"reader\",",
        "type => \"domain\",",
        "});",
        "# generate a multipart request to send all of the other requests",
        "my $r = HTTP::Request->new('POST', $batchurl, [",
        "'Accept-Encoding' => 'gzip',",
        "# if we don't provide a boundary here, HTTP::Message will generate",
        "# one for us. We could use UUID::uuid() here if we wanted.",
        "'Content-Type' => 'multipart/mixed; boundary=ENDOFPART'",
        "]);",
        "# add the two POST requests to the main request",
        "$r->addpart($req1, $req2);",
        "# at this point, we could send it via LWP::UserAgent",
        "# my $ua = LWP::UserAgent->new();",
        "# my $res = $ua->request($r);",
        "exit();",
        "sub buildjsonrequest {",
        "my ($url, $href) = @;",
        "my $header = ['Authorization' => \"Bearer $authtoken\", 'Content-Type' => 'application/json; charset=UTF-8'];",
        "return HTTP::Request->new('POST', $url, $header, encodejson($href));"
    ],
    "see_also": []
}