Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

matlab.net.http.RequestMessage class

Package: matlab.net.http
Superclasses: matlab.net.http.Message

HTTP request message

Description

Use the RequestMessage class to format HTTP request messages to send to a server for processing. Use the send method to send the message, or the complete method to validate the message before sending. These methods fill in any necessary header fields and other message properties.

Construction

obj = matlab.net.http.RequestMessage creates a request message with default values. When you send or complete a message, the default Method property is RequestMethod.GET.

example

obj = matlab.net.http.RequestMessage(method,header,body) specifies one or more optional message properties. You can omit trailing arguments and use [] to specify any placeholders.

obj = matlab.net.http.RequestMessage(requestLine,header,body) sets the RequestLine property to requestLine. Use this syntax if you need control over the contents of the request line. For example, to send a message explicitly to a proxy, set the RequestLine.RequestTarget property to the full URI. Otherwise, MATLAB chooses the proxy based on your proxy settings, and the send method sets the RequestTarget to the Path property of the URI.

Properties

expand all

Request line, specified as a matlab.net.http.RequestLine object or a string or character vector that contains the method, target, and protocol version. This line is automatically created when you send a message, based on the method and URI you specify. If you set this property explicitly, then its contents are used as the request line. The value might be set to a RequestLine object or to a string which is parsed and converted to a RequestLine object.

Example: 'GET HTTP/1.1'

Attributes:

Dependenttrue

Request method, specified as a matlab.net.http.RequestMethod object, a string, a character vector, or empty ([]). To specify a method, set the RequestMessage.Method property or the RequestLine.Method property.

Example: matlab.net.http.RequestMethod.CONNECT

Example: 'get'

Attributes:

Dependenttrue
GetAccesspublic
SetAccesspublic

Message header, specified as a matlab.net.http.HeaderField object or a vector of HeaderField objects. When you set the Header property, MATLAB® checks the fields of the header to ensure that they are appropriate for the message type. The RequestMessage send and complete methods fill in any required header fields for a properly formed request.

Message body, specified as a matlab.net.http.MessageBody object or data acceptable to the MessageBody constructor. By default, Body is empty (set to []). A request message containing a Body property normally uses a method such as 'PUT' or 'POST', not the default value 'GET', but this syntax is not enforced.

In a completed or received message, if the message has a ContentTypeField header field, then the MessageBody.ContentType property is set to that value. Otherwise, ContentType is unchanged or empty.

Whether message is complete, specified as true or false. A true value means that the message was completed.

Methods that validate messages (RequestMessage.send and RequestMessage.complete) set the Completed property to true after:

  • Determining that the message is valid.

  • Completing processing, such as adding required header fields and converting the data.

If the property is true, then these methods do not modify the message, and the send method sends the message without checking it for validity. Any change to this message after that changes the Completed property back to false.

To send arbitrary headers and data in a request message, set Completed to true to prevent the send method from modifying the message. You can still use the complete method to validate the message, but the send method sends it whether it is valid.

If a request message contains data (the Body.Data property is not empty), then Completed is set to true only if Body.Payload contains the raw data. In a response message, the payload is set only if you specify the HTTPOptions.SavePayload property.

Attributes:

Transienttrue

Data Types: logical

Methods

completeValidate and complete HTTP request message without sending
sendSend HTTP request message and receive response

Attributes

Sealedtrue

Examples

expand all

Format an HTTP message requesting a server to add text to a website. This example only formats the message and does not send the data.

Add content to the message body.

data = 'Data to send';
body = matlab.net.http.MessageBody(data);
body.show
Data to send

Create a Content-Type header field describing the data type of the body.

contentTypeField = matlab.net.http.field.ContentTypeField('text/plain');

Create an Accept header field specifying the data types acceptable in the response message.

type1 = matlab.net.http.MediaType('text/*');
type2 = matlab.net.http.MediaType('application/json','q','.5');
acceptField = matlab.net.http.field.AcceptField([type1 type2]);

Create a request header containing the two header fields.

header = [acceptField contentTypeField];

Specify that this message is a PUT request.

method = matlab.net.http.RequestMethod.PUT;

Create the request message and display the contents.

request = matlab.net.http.RequestMessage(method,header,body);
show(request)
PUT
Accept: text/*, application/json; q=.5
Content-Type: text/plain

Data to send

Introduced in R2016b

Was this topic helpful?