HTTP Data Type Conversion
The MATLAB® HTTP interface automatically converts data types used in HTTP messages to and from MATLAB types.
Converting Data in Request Messages
When sending a message with a payload, assign your MATLAB data to the Data
property in a MessageBody
object, then send it
as a Body
property in a RequestMessage
object. The type of
your MATLAB data depends on the HTTP Content-Type of the message. If you do not
specify a Content-Type, then MATLAB assumes Content-Type values, as described in Content-Type Not Specified.
This table shows how MATLAB converts Data
to a payload in a request message
based on the type/subtype properties and the charset attribute that you specify in
the Content-Type header field. The asterisk character (*
) means
any subtype.
Content-Type | MATLAB Type in |
---|---|
application/json |
If you already have JSON-encoded text, then
assign the text to the If the charset
attribute is not specified, then the default charset value is
|
| If If If you did not specify a charset, the
default depends on the subtype. For the following subtypes, the
default is
For all other subtypes, MATLAB determines the charset. If all characters are in
the ASCII range, then the charset is
Note Servers might not properly interpret text types encoded as
|
image/* |
To control the conversion of your image data or
to override the type of conversion based on the subtype, specify
additional arguments to For example, the following code converts
body = MessageBody({imageData,'jpg','Quality',50}); req = RequestMessage('put',ContentTypeField('image/jpeg'),body); resp = req.send(url); |
| If If
|
| If |
audio/* |
MATLAB supports the following audio types:
|
|
For For the
other types, MATLAB converts To specify
additional name-value pair arguments to
|
Content-Type Not Specified
If you do not specify a Content-Type field in the request message, MATLAB assigns the type, subtype, and charset based on the type of the
Data
property. This assumed behavior might not result
in the Content-Type you intended, or might fail to determine the type, so for
best results, specify the Content-Type. The following table describes the
assumed Content-Type based on Data
. Types not listed might
be handled, but the behavior for unlisted types is not guaranteed to remain the
same in future releases.
| Resulting Content-Type |
---|---|
string | text/plain |
table | text/csv |
cell vector whose first element is a table |
|
org.w3c.dom.Document | application/xml |
uint8 vector | To send a To send
character-based data with no conversion, use the
|
If the type is not one of those listed in the table, then MATLAB determines whether it is one of the following character-based types:
text/*
any type with a charset
application/*javascript
application/vnd.wolfram.mathematica.package
MATLAB converts these types to a string, using the charset, if specified,
or US-ASCII
for text/plain
, UTF-8 for the
application types, and the default MATLAB encoding for the other types.
Converting Data in Response Messages
When receiving a message with a payload, MATLAB converts the incoming byte stream
(MessageBody.Data
property) to an appropriate MATLAB type.
The following table is a list of the Content-Types that MATLAB recognizes in a response message, based the type/subtype properties
and the charset attribute in the received Content-Type field. MATLAB converts the data only if the
HTTPOptions.ConvertResponse
property is true, which is the
default. In the table, the asterisk character (*
) means any
characters.
Response Message Content-Type | MATLAB Type in MessageBody.Data
Property |
---|---|
application/json |
|
image/* |
For
the supported types of image data, see Supported Image Data Subtypes. If the
subtype is not in this list, then the subtype is passed to
|
audio/* |
|
|
|
application/*spreadsheet* |
|
text/xml application/xml | If Java is available, If
Java is not available, |
If the type is not one of those listed in the table, then MATLAB determines whether it is one of the following character-based types:
text/*
any type with a charset
application/*javascript
application/vnd.wolfram.mathematica.package
MATLAB converts these types to a string, using the charset, if specified, or
US-ASCII
for text/plain
, UTF-8 for the
application types, and the default MATLAB encoding for the other types.
If MATLAB does not support the type, or if the
HTTPOptions.ConvertResponse
property is set to
false
, then:
If the type is character-based, then
Data
contains the payload converted to string.Otherwise,
Data
contains the rawuint8
vector.
If conversion of incoming data is attempted but fails (for example,
"image/jpeg"
data is not valid JPEG data), then the History
property in the
HTTPException
thrown by the RequestMessage.send
method contains the ResponseMessage
with the
Payload
property set to the uint8
payload and, if the type is character-based, then Data
is set
to the payload converted to a string.
Supported Image Data Subtypes
The following subtypes are supported by the imwrite
function
as the specified format. For example, the format argument for subtype
bmp
is 'bmp'
. The
imread
function converts the data with the specified
subtype as the format.
Subtype | Format Used
by |
---|---|
| 'bmp' |
| 'gif' |
| 'jpeg' |
| 'jp2' |
| 'jpx' |
| 'png' |
| 'tiff' |
| 'hdf' |
| 'pbm' |
| 'pcx' |
| 'pgm' |
| 'pnm' |
| 'ppm' |
| 'ras' |
| 'xwd' |
See Also
MessageBody
| RequestMessage
| ResponseMessage
| ContentTypeField
| HTTPException
| HTTPOptions
| imwrite
| imread
| audiowrite
| audioread
| jsonencode
| jsondecode
| xmlwrite
| xmlread