Send Multipart Form Messages
The following code sends one file in a PUT message.
import matlab.net.http.* import matlab.net.http.field.* import matlab.net.http.io.* provider = FileProvider('dir/imageFile.jpg'); req = RequestMessage('PUT',[],provider); resp = req.send(url);
The provider sets the appropriate Content-Type header field in the request message to
the type of file derived from the file name extension, and adds a Content-Disposition
field naming the file. In this example, the values are "image/jpeg"
with file name "imageFile.jpg"
.
To upload multiple files in a "multipart/mixed"
message, possibly
of different types, create an array of FileProviders
by specifying an
array of file names and use this array as a delegate to a
MultipartProvider
. In the following code, each header of the
multipart message contains a Content-Type field and a Content-Disposition field with a
file name attribute naming the file.
url = "www.somewebsite.com"; provider = MultipartProvider(FileProvider(["image1.jpg", "file1.txt"])); req = RequestMessage('PUT', [], provider); resp = req.send(url);
In practice, most servers that accept multipart content expect it to be of type
"multipart/form-data"
, not "multipart/mixed"
.
To send files using multipart forms, use a MultipartFormProvider
. That
provider requires you to know the control names for the various fields of the form, so
that each part is associated with the correct control. For example, to send a form with
controls called "files"
and "text"
, where the
first accepts multiple files and the second accepts just one, create a provider based on
the following code:
provider = MultipartFormProvider("files", FileProvider(["image1.jpg", "file1.txt"]),... "text", FileProvider("file1.txt"));
If a server requires multiple files specified by using a nested format, use the following code pattern:
provider = MultipartFormProvider("files", MultipartProvider(FileProvider(["image1.jpg", "file1.txt"])),... "text", FileProvider("file1.txt"));