matlab.net.http.HeaderField - Header field of HTTP message - MATLAB (original) (raw)

Namespace: matlab.net.http

Header field of HTTP message

Description

Use the HeaderField class to implement a header field for an HTTP message. The class provides conversions between strings in the header and MATLABĀ® objects, arrays, and structures. Although you can set theHeaderField properties to arbitrary values, HTTP header fields have constraints on the allowed characters.

The Name property defines the header field type. MATLAB provides subclasses for commonly used fields in thematlab.net.http.field namespace. To see a list of supported subclasses, call the HeaderField.displaySubclasses method.

Creation

Description

obj = matlab.net.http.HeaderField(name,value) creates a header field with the Name property set toname and the Value property set tovalue. Either argument can be an empty double, []. You can specify several argument pairs in any order asname1,value1,...,nameN,valueN. If the last value argument is missing, then HeaderField treats it as empty.

example

Properties

expand all

Header field name, specified as a string or character vector.Name determines the type of the field, which determines valid values for the Value property. If you set Name to [] or an empty string, then Value is[].

If this object is an instance of a subclass implementing a specific header field type, then that class enforces constraints on the Name property.

Example: 'Content-Type'

Attributes:

GetAccess public
SetAccess public

Header field value, specified as a string or any type valid for theName property.

When you read this property, Value is a string representing the value in the field.

When you set this property, Value is any type acceptable to the field based on the Name property and/or the class of this object. The result is converted to a string. If a field type has a default value, setValue to an empty string ('' orstring('')). If you specify an empty double, [], then the request message send and complete methods do not add this field to the message.

Example: 'text/html'

Attributes:

GetAccess public
SetAccess public
Dependent true

Methods

expand all

convert Convert header field value to MATLAB type
convertLike Convert header field value like another header field
parse Parse header field value and return as strings
addFields Add fields to HeaderField array
removeFields Remove fields from header field array
changeFields Change existing values in HeaderField array
replaceFields Change values in or add fields to array of HeaderFields
getFields Return header fields matching name or class
eq Compare two HeaderField arrays
displaySubclasses Display supported HeaderField subclasses

These methods specialize standard MATLAB operators and functions for objects in this class.

isequal Return true if two header field arrays are the same size and the corresponding elements are equal as described by the eq method.
string Array of header fields as a string, as it appears in a message. Newline characters are inserted between the fields but not at the end of all the fields.
char Array of header fields as a character vector, as described by thestring method

Examples

collapse all

To create a Content-Type header field, use either theHeaderField class or the ContentTypeField class constructor.

When you use the HeaderField class constructor, you specify theName property as 'Content-Type'. However, if you misspell the field name, you might not find out about the error until the server rejects the message. Some servers silently ignore unknown field names.

f1 = matlab.net.http.HeaderField('Content-Type','text/plain');

Using the ContentTypeField class constructor is preferred because you cannot misspell the field name.

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

If the Value properties are the same, then the fields are equal, regardless of which constructor you use.

This example shows how to locate a specific header field Cache-Control in a response from mathworks.com.

Send a message to mathworks.com.

request = matlab.net.http.RequestMessage; uri = matlab.net.URI('https://www.mathworks.com'); response = send(request,uri);

Search for Cache-Control and display the value.

field = response.getFields('Cache-Control'); value = field.Value

Tips

Version History

Introduced in R2016b