HTTP::R...::Common(3)
๐ HTTP::R...::Common โ like, the perl manual page, but make it make sense ๐ฆ
okay bestie, this is a User Contributed Perl Documentation page, and we're about to make it cute. it's literally called HTTP::R...::Common(3) in the header, but the module's actual name is HTTP::Request::Common. slay.
โจ NAME โจ
HTTP::Request::Common โ Construct common HTTP::Request objects ๐ธ
๐ VERSION ๐
version 7.03 โ periodt ๐
๐ SYNOPSIS ๐
okay so the whole vibe in one code block, no cap:
use HTTP::Request::Common;
$ua = LWP::UserAgent->new;
$ua->request(GET 'http://www.sn.no/');
$ua->request(POST 'http://somewhere/foo', foo => bar, bar => foo);
$ua->request(PATCH 'http://somewhere/foo', foo => bar, bar => foo);
$ua->request(PUT 'http://somewhere/foo', foo => bar, bar => foo);
$ua->request(OPTIONS 'http://somewhere/foo', foo => bar, bar => foo);
๐ DESCRIPTION ๐
bestie, this module hands you functions that return freshly-created HTTP::Request objects. the way that these are just so much more convenient than the standard HTTP::Request constructor for the requests you make all the time? iconic. ๐ฅ
ngl, also worth knowing: LWP::UserAgent has several convenience methods, including get, head, delete, post and put. so if you're lazy (respectfully), there's that too.
here are all the functions it gives you, in order, no skipping:
GET
GET $url
GET $url, Header => Value,...
the GET function returns an HTTP::Request object initialized with the GET method and the URL you gave it. it's roughly equivalent to this call:
HTTP::Request->new(
GET => $url,
HTTP::Headers->new(Header => Value,...),
)
...but, like, way less cluttered. ๐ซถ
what's different though (iykyk): a header named Content will initialize the content part of the request instead of setting a header field. GET requests should normally not have a content, so this hack makes way more sense for the PUT, PATCH and POST functions described below. ๐
the get(...) method of LWP::UserAgent exists as a shortcut for $ua->request(GET ...).
HEAD
HEAD $url
HEAD $url, Header => Value,...
like GET() but the method in the request is HEAD. that's it, that's the tea. โ
the head(...) method of LWP::UserAgent exists as a shortcut for $ua->request(HEAD ...).
DELETE
DELETE $url
DELETE $url, Header => Value,...
like GET but the method in the request is DELETE. lowkey important: this function is not exported by default. โ ๏ธ
PATCH
PATCH $url
PATCH $url, Header => Value,...
PATCH $url, $form_ref, Header => Value,...
PATCH $url, Header => Value,..., Content => $form_ref
PATCH $url, Header => Value,..., Content => $content
the same as POST below, but the method in the request is PATCH. copy-paste energy, but make it a verb. ๐
PUT
PUT $url
PUT $url, Header => Value,...
PUT $url, $form_ref, Header => Value,...
PUT $url, Header => Value,..., Content => $form_ref
PUT $url, Header => Value,..., Content => $content
the same as POST below, but the method in the request is PUT. ๐ฆ
OPTIONS
OPTIONS $url
OPTIONS $url, Header => Value,...
OPTIONS $url, $form_ref, Header => Value,...
OPTIONS $url, Header => Value,..., Content => $form_ref
OPTIONS $url, Header => Value,..., Content => $content
the same as POST below, but the method in the request is OPTIONS.
okay heads up: this was added in version6.21, so you should require that in your code:use HTTP::Request::Common 6.21;
POST
POST $url
POST $url, Header => Value,...
POST $url, $form_ref, Header => Value,...
POST $url, Header => Value,..., Content => $form_ref
POST $url, Header => Value,..., Content => $content
POST, PATCH and PUT all work with the same parameters. periodt.
%data = ( title => 'something', body => something else' );
$ua = LWP::UserAgent->new();
$request = HTTP::Request::Common::POST( $url, [ %data ] );
$response = $ua->request($request);
they take a second optional array or hash reference parameter $form_ref. the content can also be specified directly using the Content pseudo-header, and you may also provide the $form_ref this way. so many routes, all valid. โจ
the Content pseudo-header steals a bit of the header field namespace, because there is literally no way to directly specify a header that is actually called Content. if you really need this you must update the request returned in a separate statement. sorry bestie, that's just how it is. ๐ญ
the $form_ref argument can be used to pass key/value pairs for the form content. by default we will initialize a request using the application/x-www-form-urlencoded content type. this means you can emulate an HTML <form> POSTing like this:
POST 'http://www.perl.org/survey.cgi',
[ name => 'Gisle Aas',
email => 'gisle@aas.no',
gender => 'M',
born => '1964',
perc => '3%',
];
this will create an HTTP::Request object that looks like this:
POST http://www.perl.org/survey.cgi
Content-Length: 66
Content-Type: application/x-www-form-urlencoded
name=Gisle%20Aas&email=gisle%40aas.no&gender=M&born=1964&perc=3%25
multivalued form fields can be specified by either repeating the field name or by passing the value as an array reference. two options, both ate. ๐
the POST method also supports the multipart/form-data content used for Form-based File Upload as specified in RFC 1867. you trigger this content format by specifying a content type of 'form-data' as one of the request headers. if one of the values in the $form_ref is an array reference, then it is treated as a file part specification with the following interpretation:
[ $file, $filename, Header => Value... ]
[ undef, $filename, Header => Value,..., Content => $content ]
the first value in the array ($file) is the name of a file to open. this file will be read and its content placed in the request. the routine will croak if the file can't be opened. use an undef as $file value if you want to specify the content directly with a Content header. the $filename is the filename to report in the request. if this value is undefined, then the basename of the $file will be used. you can specify an empty string as $filename if you want to suppress sending the filename when you provide a $file value.
if a $file is provided by no Content-Type header, then Content-Type and Content-Encoding will be filled in automatically with the values returned by LWP::MediaTypes::guess_media_type(). the module just knows, bestie. ๐ฎ
sending my ~/.profile to the survey used as example above can be achieved by this:
POST 'http://www.perl.org/survey.cgi',
Content_Type => 'form-data',
Content => [ name => 'Gisle Aas',
email => 'gisle@aas.no',
gender => 'M',
born => '1964',
init => ["$ENV{HOME}/.profile"],
]
this will create an HTTP::Request object that almost looks this (the boundary and the content of your ~/.profile is likely to be different):
POST http://www.perl.org/survey.cgi
Content-Length: 388
Content-Type: multipart/form-data; boundary="6G+f"
--6G+f
Content-Disposition: form-data; name="name"
Gisle Aas
--6G+f
Content-Disposition: form-data; name="email"
gisle@aas.no
--6G+f
Content-Disposition: form-data; name="gender"
M
--6G+f
Content-Disposition: form-data; name="born"
1964
--6G+f
Content-Disposition: form-data; name="init"; filename=".profile"
Content-Type: text/plain
PATH=/local/perl/bin:$PATH
export PATH
--6G+f--
if you set the $DYNAMIC_FILE_UPLOAD variable (exportable) to some TRUE value, then you get back a request object with a subroutine closure as the content attribute. this subroutine will read the content of any files on demand and return it in suitable chunks. this allow you to upload arbitrary big files without using lots of memory. you can even upload infinite files like /dev/audio if you wish โ however, if the file is not a plain file, there will be no Content-Length header defined for the request. not all servers (or server applications) like this. ๐ฌ
also, if the file(s) change in size between the time theContent-Lengthis calculated and the time that the last chunk is delivered, the subroutine willCroak. so like... don't do that. ๐
the post(...) method of LWP::UserAgent exists as a shortcut for $ua->request(POST ...).
๐ SEE ALSO ๐
HTTP::Request, LWP::UserAgent
also, there are some examples in "EXAMPLES" in HTTP::Request that you might find useful. for example, batch requests are explained there. go look, fr fr. ๐
๐ AUTHOR ๐
Gisle Aas <gisle@activestate.com>
๐ง COPYRIGHT AND LICENSE ๐ง
this software is copyright (c) 1994 by Gisle Aas.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. ๐ซถ
and that's the whole page, bestie! this beauty is perl v5.42.2, dated 2026-07-23, from HTTP::R...::Common(3). main character energy from start to finish, no cap. bye pookie ๐๐
โจ
๐ฆ where this page comes from
- fedora 43: perl-HTTP-Message 7.03-1.fc43 ยท (GPL-1.0-or-later OR Artistic-1.0-Perl) AND CC0-1.0
- fedora 44: perl-HTTP-Message 7.03-1.fc44 ยท (GPL-1.0-or-later OR Artistic-1.0-Perl) AND CC0-1.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms