Protocol::HTTP::Request - HTTP request class
use Protocol::HTTP::Request; # construction of new request my $req = Protocol::HTTP::Request->new({ method => METHOD_POST, uri => "http://crazypanda.ru/hello/world", http_version => 10, headers => {MyHeader => "my value"}, cookies => {Lorem => 'ipsum'}, body => "my body", }); $req->method(); # => METHOD_POST $req->uri(); # => isa 'URI::XS' $req->uri->to_string; # => 'http://crazypanda.ru/hello/world' $req->uri('/something'); $req->uri->to_string; # => '/something' # we accept GZIP-compressed replies $req->allow_compression(Protocol::HTTP::Compression::gzip); $req->cookie('Location', 'Earth'); $req->cookie('Location'); # => 'Earth' $req->cookies; # => is_deeply {Location => 'Earth', 'Lorem' => 'ipsum'} # main serialization method $req->to_string; # like qr/GET.*HTTP/1.0.*MyHeader.*my body/sm # we are crazy enough to send compressed request $req->compress(Protocol::HTTP::Compression::gzip, Protocol::HTTP::Compression::LEVEL_OPTIMAL); # now $req->to_string would return a little bit different result
This class represents client HTTP request, which is specialization of Protocol::HTTP::Message. An instance of the class can be constructed either direcly via new method to send a new request (clients), or via parsing incoming request with Protocol::HTTP::RequestParser (servers).
new
If it is acceptable to have a server reply with compressed payload, then allow_compression method should be invoked. It will setup Accept-Encoding header in a request.
allow_compression
Accept-Encoding
When a new request is ready it can be serialized via to_string method into byte octets.
to_string
Constructs new request from hashref of properties, i.e. method, uri, allow_compression, headers, body, http_version, chunked, compress.
method
uri
headers
body
http_version
chunked
compress
See corresponding methods documentation below and in Protocol::HTTP::Message to find out what these parameters support.
allow_compression should be an array ref if multiple values are passed.
Get/set HTTP request method, e.g. GET, POST etc. in the first line of the request
GET
POST
Possible values:
Set/get uri as URI::XS. $uri argument can be anything that one-argument constructor of URI::XS supports (for example, string).
$uri
Set/get all cookies at once as a hashref.
$req->cookies({coo1 => "val1", coo2 => "val2", ... });
Please note, this is request cookies, i.e. set by client-side, and they have different API than response cookies.
Set/get single cookie.
Sets acceptable compression methods in the responce of the request, i.e. Accept-Encoding header. Order of compression methods might be important.
$request->allow_compression(Protocol::HTTP::Compression::gzip);
See Protocol::HTTP::Compression for the list of available compressions.
Returns the bit mask of desirable compression methods (i.e. specified at Accept-Encoding header).
if ($request->allowed_compression & Protocol::HTTP::Compression::gzip) { ...; }
Serializes a request into string for sending via network. If the compression was requested (see Protocol::HTTP::Message), then it will be applied here.
Returns stringified HTTP request method, e.g. "GET", "POST" etc.
"GET"
"POST"
Returns corresponding string for a constant METHOD_*, i.e. "GET", "POST" etc.
METHOD_*
Protocol::HTTP
Protocol::HTTP::Message
Protocol::HTTP::Compression
URI::XS
To install Protocol::HTTP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Protocol::HTTP
CPAN shell
perl -MCPAN -e shell install Protocol::HTTP
For more information on module installation, please visit the detailed CPAN module installation guide.