HTTP::Promise::Body::Form::Data - A multipart/form-data Representation Class
use HTTP::Promise::Body::Form; my $form = HTTP::Promise::Body::Form::Data->new; my $form = HTTP::Promise::Body::Form::Data->new({ fullname => 'Jigoro Kano', location => HTTP::Promise::Body::Form::Data->new_field( name => 'location', value => 'Tokyo', ), picture => HTTP::Promise::Body::Form::Data->new_field( name => 'picture', file => '/some/where/file.txt', ), }); my $form = HTTP::Promise::Body::Form::Data->new( $hash_ref ); my $form = HTTP::Promise::Body::Form::Data->new( q{e%3Dmc2} ); die( HTTP::Promise::Body::Form->error, "\n" ) if( !defined( $form ) );
v0.1.0
This class represents a form-data content as key-value pairs and is designed to make construction and manipulation of multipart/form-data easier. It inherits from HTTP::Promise::Body::Form
form-data
multipart/form-data
For x-www-form-urlencoded, use HTTP::Promise::Body::Form instead.
x-www-form-urlencoded
Each key represents a form-data field and its value can either be a simple string or a HTTP::Promise::Body::Form::Field object.
HTTP::Promise::Body::Form::Field
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
This takes an optional data, and some options and returns a new HTTP::Promise::Body::Form object.
Acceptable data are:
If a string is provided, it will be automatically decoded into an hash of name-value pairs. When a name is found more than once, its values are added as an array reference.
my $form = HTTP::Promise::Body->new( 'name=John+Doe&foo=bar&foo=baz&foo=' );
Would result in a HTTP::Promise::Body::Form object containing:
HTTP::Promise::Body::Form
name => 'John Doe', foo => ['bar', 'baz', '']
HTTP::Promise::Body::Form inherits all the methods from Module::Generic::Hash, and adds or override the following ones.
Provided with an hash or hash reference of options and this returns a scalar object of the form-data properly formatted as multipart elements.
Be mindful of the size of the parts and that this is not cached, so each time this is called, it creates the parts.
Supported options are:
boundary
A string used as a part delimiter. Note, however, that even if you provide this value, it will not replace the boundary value of a HTTP::Promise::Body::Form::Field Content-Disposition field if it is set.
Content-Disposition
If this is not provided, a new one will be automatically generated using "create_str" in Data::UUID
eol
The end-of-line terminator. This defaults to \015\012
\015\012
fields
An array reference of form field names. This is used to set the order of appearance.
If not provided, it will default to alphabetic order.
This returns a new HTTP::Promise::Body::Form object based on the current data, or upon error, sets an error and returns undef.
undef
This takes an hash or hash reference of options and creates entity part objects and returns them as an array object
Provided with an hash or hash reference of options and this returns an array object of parts
Note that at this point, the body is not encoded and the Content-Length is not added. You can use "encode_body" in HTTP::Promise::Entity on each part to encode a form part value.
Content-Length
This takes an hash or hash reference of options and returns the new HTTP::Promise::Body::Form::Data object, or upon error, sets an error and returns undef.
HTTP::Promise::Body::Form::Data
headers
This is optional. Either as HTTP::Promise::Headers object or as an array reference.
name
Field name
value
Field value as a string, scalar reference or a file object
This transform all the form-data elements into a proper multipart/form-data using "as_string" and returns a new Module::Generic::Scalar::IO object.
It then opens the scalar passing "open" in Module::Generic::Scalar whatever arguments were provided and returns an Module::Generic::Scalar::IO object.
Sets or gets an array object of form fields in the desired order of appearance when stringified.
Provided with a valid filehandle, and this print the form-data representation of the form fields and their values, to the given filehandle, or upon error, sets an error and returns undef
Jacques Deguest <jack@deguest.jp>
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
Module::Generic::Scalar
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.