send
Class: matlab.net.http.RequestMessage
Namespace: matlab.net.http
Send HTTP request message and receive response
Syntax
Description
[
sends the response
,completedrequest
,history
] = send(request
,uri
)request
message to the web service specified by
uri
and returns the response
, if any.
If no request.Method
property is specified, then the
send
method sets the property to
'GET'
.
By default, send
verifies the semantic correctness of the headers
and other parts of the message and completes the uri
. The
method also fills in any required header fields for a properly formed request. If
request.Body
is a MessageBody
whose
Payload
property is not already set, then
send
calls appropriate conversion functions to convert any
request.Body.Data
to a vector of bytes representing an HTTP
payload to be sent, as described for MessageBody.Data
.
Normally, a 'GET'
request does not contain data, but the method
sends the Body
regardless of the
RequestMethod
. If the server returns data in its response
and no consumer
is specified, then send
converts that data to MATLAB® data and saves it in
response.Body.Data
. See MessageBody.Data
for more information on data conversion.
If request.Body
is a ContentProvider
, then
MATLAB calls the provider to get the data to be sent.
If the header already contains a field that the method normally adds, then
send
verifies that the field has the expected value. You can
override the default behavior as follows.
To send a message as is without any checking or alteration of the header, set the
request.Completed
property totrue
before sending. If you used thecomplete
method to complete the request, then you should specify the same value ofuri
andoptions
that you provided tocomplete
, or there might be unpredictable results. Even ifCompleted
is set, unspecified fields in theRequestLine
will be filled in with default values.To allow the
send
method to check and alter the header, but suppress adding a particular header field thatsend
or aContentProvider
might add, add that field torequest.Header
with an empty value ([]
). For example,send
automatically adds a User-Agent header field. If you do not want this behavior, then addHeaderField('User-Agent')
to the header. Header fields with empty values are not included in the message. The Host and Connection fields cannot be suppressed.To override the value that the
send
method adds for a given header field, add your own instance of that field before sending or completing the message. However, this will not override a header field that aContentProvider
might add. However, for some header field types,send
might still reject the message if the value is not valid. To prevent any checking of the value of a given field, or to override a field that aContentProvider
adds, add a field of typematlab.http.field.GenericField
to the header with the desired name and value. Neithersend
nor aContentProvider
will add any header fields with names equal to anyGenericField
headers and will not check their correctness.To send raw binary data without conversion, you can insert a
uint8
vector into eitherBody.Data
orBody.Payload
. The only difference is that data inBody.Data
is subject to conversion based on the Content-Type field in the message, whileBody.Payload
is not.send
always tries to convert nonemptyBody.Data
ifBody.Payload
is empty, even ifCompleted
is already set. SeeMessageBody.Data
for conversion rules.
Input Arguments
Output Arguments
Examples
Error Handling
Always check the response
Status
property
to determine whether the request was accepted. Error conditions are:
MException
— Message is not well formed and cannot be completed.HTTPException
— Message is completed, but the web service is unreachable or does not respond within the timeout period specified inoptions
.Status
property ofresponse
— Web service responds and returns an HTTP error status.send
returns normally, setting theStatus
property to the error returned from the server.
Version History
Introduced in R2016b