matlab.net.http.io.ContentProvider Class
Namespace: matlab.net.http.io
Superclasses: handle
, matlab.mixin.Heterogeneous
ContentProvider for HTTP message payloads
Description
A ContentProvider supplies data for an HTTP
RequestMessage
while the message is being sent. A simple provider converts
data from a MATLAB® type to a byte stream. More complex providers can stream data to the server,
obtaining or generating the data at the same time it is being sent, which avoids the need to
have all the data in memory before the start of the message.
Normally, when sending data to a web service (typically in a PUT or POST request), you
would create a RequestMessage
and insert data in the form of a
MessageBody
object in the RequestMessage.Body
property.
When you send that message using RequestMessage.send
, MATLAB converts that data into a byte stream to be sent to the server, converting it
based on the Content-Type of the message and the type of data in
Body.Data
. See MessageBody.Data
for these
conversion rules.
Instead of inserting a MessageBody
object into the
RequestMessage.Body
property, you can create a
ContentProvider
object and insert that instead. Then, when you send the
message, MATLAB calls methods in the ContentProvider
to obtain buffers of data to
send, while the message is being sent.
Whether you insert a MessageBody
or a ContentProvider
into
the message, the call to RequestMessage.send
does not return (that is, it is
blocked) until the entire message has been sent and a response has been received, or an error
has occurred. But with a ContentProvider
, MATLAB makes periodic callbacks into the provider to obtain buffers of data to send,
during the time send is blocked. In these callbacks, your ContentProvider
can
obtain data from any source such as a file, a MATLAB array, a hardware sensor, a MATLAB function, etc. The provider's job is to convert that data to a byte stream, in
the form of uint8 buffers, that can be sent to the web.
ContentProvider
is an abstract class designed for class authors to subclass
with their own data generator or converter, or you can use (or subclass) one of the
MATLAB providers that generate the data for you from various sources, without writing a
subclass. These providers have options that give you more flexible control over how data is
obtained and converted, compared to the automatic conversions that occur when you insert data
directly into a MessageBody
. Use one of the ContentProvider
subclasses:
Even if you do not need to stream data, using one of these providers can simplify the
process of sending certain types of content, as they convert data from an internal form into a
uint8
stream. For example, FormProvider
lets you send form
responses to a server, where you can conveniently express the data as an array of
QueryParameter
objects. MultipartFormProvider
lets you send
multipart form responses, simplifying the creation of responses to multipart forms. To use any
ContentProvider
, you need to understand the type of content that the server
expects you to send.
The matlab.net.http.io.ContentProvider
class is a handle
class.
Subclass Authors
The simplest possible ContentProvider
need only implement a
getData
method to provide buffers of data as MATLAB requests them. To use your provider, insert it into in the
Body
property of the RequestMessage
. In this example,
the third argument to the RequestMessage
constructor, a
MyProvider
object, goes into the Body
:
provider = MyProvider; req = matlab.net.http.RequestMessage('put', headers, provider); resp = req.send(uri);
Here is an example a MyProvider
class that reads from a file name
passed in as an argument to the constructor and sends it to the web. For good measure, we
close the file at the end or when this provider is deleted.
classdef MyProvider < matlab.net.http.io.ContentProvider properties FileID double end methods function obj = MyProvider(name) obj.FileID = fopen(name); end function [data, stop] = getData(obj, length) [data, len] = fread(obj.FileID, length, '*uint8'); stop = len < length; if (stop) fclose(obj.FileID); obj.FileID = []; end end function delete(obj) if ~isempty(obj.FileID) fclose(obj.FileID); obj.FileID = []; end end end end
MATLAB calls a provider's complete
method when it is forming a new
message to send. The purpose is to allow the provider to prepare for a new message and add
any required header fields to the message. MATLAB calls a provider's start
method when it is time to send the
data, but before the first call to getData
.
Properties
Methods
More About
Version History
Introduced in R2018a