matlab.net.http.MessageBody Class
Namespace: matlab.net.http
Body of HTTP message
Description
A MessageBody object contains the body of an HTTP message. In a request
message, set the Body property to your data or to a
MessageBody object containing your data. In a response message, this object
contains the received data.
Message data is represented by two properties. The Payload property
contains the raw bytes sent to or received from the network. The Data
property contains the Payload as a MATLAB® type. Often only one of these two properties is set.
Creation
A MessageBody object contains the body of an HTTP message. In a request
message, set the Body property to your data or to a
MessageBody object containing your data. In a response message, this object
contains the received data.
Message data is represented by two properties. The Payload property
contains the raw bytes sent to or received from the network. The Data
property contains the Payload as a MATLAB type. Often only one of these two properties is set.
Properties
Message data, specified as one of the following MATLAB data types. For conversion details, see HTTP Data Type Conversion.
uint8vector —Datanot converted.character array or scalar string —
Dataconverted based on the media type and its charset attribute in the Content-Type header.structure array —
Dataconverted to or from JSON string usingwebreadandwebwrite.image —
Dataconverted to or from an image usingimreadandimwrite.XML DOM —
Dataconverted to or from a string usingxmlreadandxmlwrite.audio data —
Dataconverted usingaudioreadandaudiowrite.table —
Dataconverted usingreadtableandwritetable.Other type of array or cell array —
Dataconverted to or from JSON usingjsonencodeandjsondecode.
In a request message, the Data property is the MATLAB data before conversion to a uint8 payload. Data
conversion happens when you call the RequestMessage
send or complete methods. The conversion depends on
the Content-Type you specify in the message and the type of Data.
If you do not specify a Content-Type, then the send and
complete methods try to deduce the type from the data and add the
appropriate ContentTypeField to the request message.
In a response message, Data represents the
uint8 payload converted to a MATLAB type based on the Content-Type specified by the server. If conversion
fails, then Data remains empty and the raw data appears in the
Payload property.
To suppress automatic conversion of the response Payload, set
the HTTPOptions.ConvertResponse property to
false. In that case, Data contains either a
string for character data or a uint8 vector of bytes.
Attributes:
GetAccess | public |
SetAccess | public |
Dependent | true |
Content-Type of the Data property, specified as a read-only
matlab.net.http.MediaType object. This
property determines how contents of the Data property was converted
to or from the payload. If the Content-Type has a MediaType with a
charset attribute, then the charset determines the encoding. For more information, see
RFC 2616, section 14.17 Content-Type on the RFC Editor website.
This property usually has the same values as the result of calling
convert on the Content-Type field in the message containing this
MessageBody.
You do not set ContentType in a request message. When you
create a MessageBody object, this property is empty. When you copy
MessageBody into a request message, ContentType is
set to the value of the ContentTypeField in the message, if there is one.
The RequestMessage
send and complete methods set
ContentType based on the type of Data and
the value of the ContentTypeField in the request message.
In a response message, ContentType is based on the
ContentTypeField of the message.
Attributes:
GetAccess | public |
SetAccess | public |
Transient | true |
This property is set to indicate that the Payload property of a
ResponseMessage is encoded. When ContentCoding is
set, no processing was done on the payload and the Data property is
empty.
If MATLAB receives a message whose payload is encoded using a compression algorithm
that it supports, such as gzip or deflate, it
automatically decodes that payload before attempting any other conversions. If decoding
was successful, it optionally stores the decoded payload in Payload
and the converted payload (if any) in Data. In that case, this
property is empty to indicate that the Payload is not
encoded.
If the payload was encoded but decoding was not successful, or you suppressed
decoding by setting the HTTPOptions.DecodePayload property to
false, then the unprocessed still-encoded payload is returned in
Payload, Data is left empty, and
ContentCoding is set to a vector of strings representing the
value of the Content-Encoding header field in the response message. In this case, you
can save the Payload as is (for example, write it to a file), or
process it according to the compression algorithms specified in
ContentCoding. For example, if the value is
gzip, you can write the data to a file and use the
gunzip command to process the data.
Attributes:
GetAccess | public |
SetAccess | public |
Transient | true |
Data Types: string
Raw bytes sent in a message, specified as a uint8 vector, scalar
string, or character vector. As a convenience in a request message, you can set
Payload to a scalar string or character vector. MATLAB converts the value to a uint8 vector. In a response
message, Payload is always a uint8
vector.
In a request message:
A scalar string or character vector is converted using the charset specified in or implied by the
ContentTypeproperty, if any, or theUTF-8encoding if none was set. To use a different encoding, encode it yourself, for example, using theunicode2nativefunction, and assign the resultinguint8vector toPayload.If you do not want output conversion on the byte array, then set
Payloadinstead of theDataproperty. If you setDatainstead, thenPayloadis filled in with the bytes that were sent if you specify a request or history return argument forRequestMessage.send, in the return value ofRequestMessage.complete, or in aResponseMessageif you set theHTTPOptions.SavePayloadproperty.When you set
Payload,Datais cleared. If you send a message where bothDataandPayloadare set, thenPayloadis sent andDatais ignored. Only response messages and request messages returned by thesendorcompletemethods can have both properties set at the same time.
In a response message, Payload is the raw bytes received in
these cases:
You specify a history return argument in the
RequestMessage.sendmethod.You set the
HTTPOptions.SavePayloadproperty totrue.Payloadis always set for messages appearing in aLogRecordIf conversion of the payload to MATLAB data failed. For information, check the
HTTPException.Historyproperty.
Attributes:
GetAccess | public |
SetAccess | public |
Dependent | true |
Methods
show | Display or return formatted version of message body data |
These methods specialize standard MATLAB operators and functions for objects in this class.
string | Data property as a string. Use for diagnostics or debugging. If the
If If |
char |
|
Version History
Introduced in R2016b
See Also
RequestMessage | ResponseMessage | MediaType | ContentTypeField | Data
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)