HTTP::Promise::Request - HTTP Request Class
use HTTP::Promise::Request; my $this = HTTP::Promise::Request->new || die( HTTP::Promise::Request->error, "\n" );
v0.1.0
HTTP::Promise::Request implements a similar interface to HTTP::Request, but does not inherit from it. It uses a different API internally and relies on XS modules for speed while offering more features.
HTTP::Promise::Request inherits from HTTP::Promise::Message
One major difference with HTTP::Request is that the HTTP request content is not necessarily stored in memory, but it relies on HTTP::Promise::Body as you can see below, and this class has 2 subclasses: 1 storing data in memory when the size is reasonable (threshold set by you) and 1 storing data in a file on the filesystem for larger content.
HTTP::Request
Here is how it fits in overall relation with other classes.
+-------------------------+ +--------------------------+ | | | | | HTTP::Promise::Request | | HTTP::Promise::Response | | | | | +------------|------------+ +-------------|------------+ | | | | | | | +------------------------+ | | | | | +--- HTTP::Promise::Message |---+ | | +------------|-----------+ | | +------------|-----------+ | | | HTTP::Promise::Entity | | | +------------|-----------+ | | +------------|-----------+ | | | HTTP::Promise::Body | | | +------------------------+
my $r = HTTP::Promise::Request->new( $method, $uri, $headers, $content, k1 => v1, k2 => v2); my $r = HTTP::Promise::Request->new( $method, $uri, $headers, $content, { k1 => v1, k2 => v2 }); my $r = HTTP::Promise::Request->new( $method, $uri, $headers, k1 => v1, k2 => v2); my $r = HTTP::Promise::Request->new( $method, $uri, $headers, { k1 => v1, k2 => v2 }); my $r = HTTP::Promise::Request->new( $method, $uri, k1 => v1, k2 => v2); my $r = HTTP::Promise::Request->new( $method, k1 => v1, k2 => v2); my $r = HTTP::Promise::Request->new( k1 => v1, k2 => v2 ); die( HTTP::Promise::Request->error ) if( !defined( $r ) );
Provided with an HTTP method, URI, an optional set of headers, as either an array reference or a HTTP::Promise::Headers or a HTTP::Headers object, some optional content and an optional hash reference of options (as the last or only parameter), and this instantiates a new HTTP::Promise::Request object. The supported arguments are as follow. Each arguments can be set or changed later using the method with the same name.
It returns the newly created object upon success, and upon error, such as bad argument provided, this sets an error and returns undef
undef
It takes the following arguments:
$method
This is a proper HTTP method in upper case. Note that you can also provide non-standard method in any case you want.
$uri
The request uri such as / or an absolute uri, typical for making request to proxy, such as https://example.org/some/where
/
https://example.org/some/where
$headers
An HTTP::Promise::Headers object or an array reference of header field-value pairs, such as:
my $r = HTTP::Promise::Request->new( $method, $uri, [ 'Content-Type' => 'text/html; charset=utf-8', Content_Encoding => 'gzip', ]);
$content
$content can either be a string, a scalar reference, or an HTTP::Promise::Body object (HTTP::Promise::Body::File and HTTP::Promise::Body::Scalar)
Each supported option below can also be set using its corresponding method.
Supported options are:
content
Same as $content above.
headers
Same as $headers above.
method
Same as $method above.
protocol
The HTTP protocol, such as HTTP/1.1 or HTTP/2
HTTP/1.1
HTTP/2
uri
The request uri, such as /chat or it could also be a fully qualified uri such as wss://example.com/chat
/chat
wss://example.com/chat
version
The HTTP protocol version. Defaults to 1.17
1.17
This sets and returns the header Accept-Encoding after having set it the value of "decodable"
Accept-Encoding
This is inherited from HTTP::Promise::Message. See "add_content" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "add_content_utf8" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "add_part" in HTTP::Promise::Message
Depending on whether uri_absolute is true, this returns a HTTP request with an URI including the HTTP host (a.k.a absolute-form) or only the absolute path (a.k.a origin-form). The former is used when issuing requests to proxies.
absolute-form
origin-form
origin-form:
GET /where?q=now HTTP/1.1 Host: www.example.org
absolute-form:
GET http://www.example.org/pub/WWW/TheProject.html HTTP/1.1
See rfc7230, section 5.3
This is inherited from HTTP::Promise::Message and returns the multipart boundary currently set in the Content-Type header.
Content-Type
This is inherited from HTTP::Promise::Message. See "can" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "clear" in HTTP::Promise::Message
This clones the current object and returns the clone version.
This is inherited from HTTP::Promise::Message. See "content" in HTTP::Promise::Message
Use this method with care, because it will stringify the request body, thus loading it into memory, which could potentially be important if the body size is large. Maybe you can check the body size first? Something like:
my $content; $content = $r->content if( $r->body->length < 102400 );
This is inherited from HTTP::Promise::Message. See "content_charset" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "content_ref" in HTTP::Promise::Message
Sets or gets the Cookie::Jar object. This is used to read and store cookies.
This is inherited from HTTP::Promise::Message. See "decodable" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "decode" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "decode_content" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "decoded_content" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "decoded_content_utf8" in HTTP::Promise::Message
Sets or gets the default HTTP protocol to use. This defaults to HTTP/1.1
This dumps the HTTP request and prints it on the STDOUT in void context, or returns a string of it.
STDOUT
This is inherited from HTTP::Promise::Message. See "encode" in HTTP::Promise::Message
Sets or gets an HTTP::Promise::Entity object.
This object is automatically created upon instantiation of the HTTP request, and if you also provide some content when creating a new object, an HTTP::Promise::Body object will also be created.
This is inherited from HTTP::Promise::Message. See "header" in HTTP::Promise::Message
Sets or gets a HTTP::Promise::Headers object.
A header object is always created upon instantiation, whether you provided headers fields or not.
This is inherited from HTTP::Promise::Message. See "headers_as_string" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "is_encoding_supported" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "make_boundary" in HTTP::Promise::Message
This takes either an HTTP::Promise::Body::Form object, an HTTP::Promise::Body::Form::Data object, an array reference or an hash reference of form name-value pairs and builds a multipart/form-data
multipart/form-data
If a boundary is already set in the Content-Type header field, it will be used, otherwise a new one will be generated.
boundary
Each name provided will be the form-data name for each part.
form-data
It returns the current entity object, or upon error, sets an error and returns undef.
Each value provided can be either one of the following:
In this case, the file-mime type will try to be guessed. If you prefer to be specific about the file mime-type, use the alternate hash reference below.
hash reference
For more granular control, you can provide an hash reference with the following supported properties:
encoding
The encoding to be applied to the content. This will also set the Content-Encoding for this form-data part.
Content-Encoding
Note that when provided, the encodings will be applied immediately on the form-data content, whether it is a string or a file.
file
A filepath to content for this part. The file content will not be loaded into memory, but instead will be used as-is. When it will need to be sent, it will be read from in chunks.
If this provided, the body object will be a HTTP::Promise::Body::File
filename
The filename attribute of the Content-Disposition or this form-data part.
Content-Disposition
If this is not provided and headers property is not provided, or headers is specified, but the Content-Disposition filename attribute is not set, then the file basename will be used instead.
An HTTP::Promise::Headers object or an array reference of header field-value pairs.
type
The mime-type to be used for this form-data part.
If a file is provided and this is not specified, it will try to guess the mime-type using HTTP::Promise::MIME
Note that even if this provided, and if a headers has been specified, it will not override an existing Content-Type header that would have been set.
value
The form-data value. This is an alternative to providing the form-data content as a file
Obviously you should not use both and if you do, file will take priority.
If this provided, the body object will be a HTTP::Promise::Body::Scalar
multipart/form-data is the only valid Content-Type for sending multiple data. rfc7578 in section 4.3 states: "[RFC2388] suggested that multiple files for a single form field be transmitted using a nested "multipart/mixed" part. This usage is deprecated."
See also this Stackoverflow discussion and this one too
See also HTTP::Promise::Body::Form::Data for an alternate easy way to create and manipulate form-data, and see also "as_form_data" in HTTP::Promise::Entity, which will create and return a HTTP::Promise::Body::Form::Data object.
Sets or gets the HTTP method, such as CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, TRACE which are the standard ones as defined by rfc7231, section 4.1
CONNECT
DELETE
GET
HEAD
OPTIONS
PATCH
POST
PUT
TRACE
Note that casing must be uppercase for standard methods, but non-standard ones can be whatever you want as long as it complies with the rfc7231.
This returns the current method set, if any, as an scalar object
Provided with a scalar reference of data, a glob or a file path, and an hash or hash reference of options and this will parse the data provided using "parse" in HTTP::Promise::Parser, passing it whatever options has been provided. See "parse_fh" in HTTP::Promise::Parser for the supported options.
This returns the resulting HTTP::Promise::Message object from the parsing, or, upon error, sets an error and returns undef.
Note that the resulting HTTP::Promise::Message object can be a HTTP::Promise::Request or HTTP::Promise::Response object (both of which inherits from HTTP::Promise::Message) if a start-line was found, or else just an HTTP::Promise::Message object.
This is inherited from HTTP::Promise::Message. See "parts" in HTTP::Promise::Message
This is inherited from HTTP::Promise::Message. See "protocol" in HTTP::Promise::Message
Read-only.
Returns a regular string representing the start-line containing the method, the uri and the protocol of the request.
For example:
GET / HTTP/1.1
See rfc7230, section 3.1
Sets or gets the timeout as an integer. This returns the value as an number object
timeout
Sets or gets the uri. Returns the current value, if any, as an URI object.
Boolean. Sets or gets whether "as_string" will return a request including an uri in absolute-form (with the host included) or in origin-form (only with the absolute path).
If true, it sets the former otherwise the latter. Default to false.
Returns the current "uri" in its canonical form by calling "canonical" in URI
This is inherited from HTTP::Promise::Message. See "version" in HTTP::Promise::Message
Jacques Deguest <jack@deguest.jp>
rfc7230, and rfc7231
HTTP::Promise, HTTP::Promise::Request, HTTP::Promise::Response, HTTP::Promise::Message, HTTP::Promise::Entity, HTTP::Promise::Headers, HTTP::Promise::Body, HTTP::Promise::Body::Form, HTTP::Promise::Body::Form::Data, HTTP::Promise::Body::Form::Field, HTTP::Promise::Status, HTTP::Promise::MIME, HTTP::Promise::Parser, HTTP::Promise::IO, HTTP::Promise::Stream, HTTP::Promise::Exception
Copyright(c) 2022 DEGUEST Pte. Ltd.
All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install HTTP::Promise, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTTP::Promise
CPAN shell
perl -MCPAN -e shell install HTTP::Promise
For more information on module installation, please visit the detailed CPAN module installation guide.