auto resp=ua->request(x::http::POST, "http://localhost/cgi-bin/req.pl", x::http::form::parameters::create("username", "alfred", "password", "rosebud"), "UTF-8", "file", "upload.txt");
A character set name parameter optionally follows the
HTTP
form parameter.
This encodes the form as a multipart/form-data
MIME entity, which contains the all fields from the form parameter,
in addition to the file uploads specified after the character set
parameter.
RFC 2388 specifies that each field in
a multipart/form-data
has a specified character
set. This comes from the character set parameter.
Older HTTP servers may expect and assume that the field values use
the same character set as the form that the form gets submitted from.
For maximum compatibility, always specify the same character set
as the form's.
The character set parameter also specifies the character set of the
field names and non-upload field values.
The list of files to upload with the form follows the character
set parameter. Each file can be specified in several ways:
Two parameters, a field name and a filename. The specified file gets uploaded as the field with the given name.
A field name, a file descriptor, an explicit MIME type, and the filename:
auto resp=ua->request(x::http::POST, "http://localhost/cgi-bin/req.pl", x::http::form::parameters::create("username", "alfred", "password", "rosebud"), "UTF-8", "file", x::fd::open("upload.txt"), "text/plain; charset=iso-8859-1", "dailyfile.txt");
The contents of the opened file descriptor get uploaded together with the form. The file's MIME type and filename are explicitly specified.
A field name,
a std::pair
of iterators,
an explicit MIME type, and the
filename:
std::string contents; auto resp=ua->request(x::http::POST, "http://localhost/cgi-bin/req.pl", x::http::form::parameters::create("username", "alfred", "password", "rosebud"), "UTF-8", "file", std::make_pair(contents.begin(), contents.end()), "text/plain; charset=iso-8859-1", "dailyfile.txt");
The pair of iterators specify the beginning and the ending iterator value for the contents of the uploaded file. The iterators must be, at minimum, forward iterators. Input iterators cannot be used, and result in a compile-time error. The file's MIME type and filename are explicitly specified.
An
x::http::upload
reference to a reference-counted object.
A list of files to upload gets prepared in advance, and stored in the
x::http::upload
:
auto upload=x::http::upload::create(); upload->add("file1", "test1.txt"); upload->add("file2", x::fd::base::open("test1.txt", O_RDONLY), "text/plain; charset=iso-8859-1", "test2.txt"); // ... auto resp=ua->request(x::http::POST, "http://localhost/cgi-bin/req.pl", x::http::form::parameters::create("username", "alfred", "password", "rosebud"), "UTF-8", upload);
Each call to add
() records the
file to be uploaded. add
() takes any of the
parameters that specify an individual file upload to
request
(), and passing the upload handle
in request
() uploads those files together
with the form, as if they were specified directly in the
request
().