Main Content

matlab.net.http.HeaderField Class

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 the HeaderField 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 the matlab.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 to name and the Value property set to value. Either argument can be an empty double, []. You can specify several argument pairs in any order as name1,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 the Name 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, set Value to an empty string ('' or string('')). 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

Examples

collapse all

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

When you use the HeaderField class constructor, you specify the Name 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.

f1 == f2
ans =    1

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
value = 
"max-age=14400"

Tips

  • The HeaderField constructor creates fields of class HeaderField. To create a field of a class defined in the matlab.net.http.field namespace, use the subclass constructor instead. For a list of subclasses, call the HeaderField.displaySubclasses method.

    For example, the matlab.net.http.field.DateField subclass creates a Date header field. If you use the HeaderField class to create a Date field, the DateField class interprets and enforces the value, even though HeaderField is not an instance of DateField. Likewise, if you convert the field value to a MATLAB datetime value, the DateField.convert method is used.

  • If the HeaderField constructor rejects the Name and Value arguments, use the GenericField class constructor instead.

Version History

Introduced in R2016b