Main Content

changeFields

Class: matlab.net.http.Message
Namespace: matlab.net.http

Change existing fields in message header

Description

example

msg = changeFields(msg,fields) changes the existing fields in each message to the names, values, and types specified in fields and returns the updated message. This syntax might change the class of an existing field if the field name is a case-insensitive match to a name in fields.

This method throws an error when:

  • All the specified fields are not already in the header.

  • There is more than one field with the specified name.

msg = changeFields(msg,FieldName1,FieldValue1,...,FieldNameN,FieldValueN) changes fields with the specified names to the indicated values.

Name matching is case-insensitive. However, if you specify a name that differs in case from the existing field name, then the field name changes to the specified name. This usage does not change the class of an existing field.

Input Arguments

expand all

Message, specified as a matlab.net.http.Message object.

Fields to change, specified as a vector or comma-separated list of one or more matlab.net.http.HeaderField objects.

Example: 'Accept','text/plain'

Fields defined by name-value pairs. FieldName is specified as a string or character vector, and FieldValue is specified as any type valid for FieldName.

To use the default value for the field, set FieldValue to ''.

If the last value is missing, it is the same as specifying empty ([]).

Examples

expand all

Create two Content-Length fields using the HeaderField class and its subclass, ContentLengthField. Change the value of the Content-Length field in a message, which changes the class of the field, depending on how you create the original header field.

Create two header files with the same properties. The fields have the same value, but the classes are different.

h1 = matlab.net.http.HeaderField('Content-Length',5);
h2 = matlab.net.http.field.ContentLengthField(5);
compareNames = eq(h1.Name,h2.Name)
compareNames = logical
   1

compareValues = eq(h1.Value,h2.Value)
compareValues = logical
   1

Create a request message using the h1 header field and display its class.

r = matlab.net.http.RequestMessage;
r.Header= h1;
h1Class = class(r.Header)
h1Class = 
'matlab.net.http.HeaderField'

Change the Content-Length header field using the h2 header field. The header field class changes.

r1 = r.changeFields(h2);
h2Class = class(r1.Header)
h2Class = 
'matlab.net.http.HeaderField'

Change the Content-Length header field using a name-value pair. The header field class does not change.

r1 = r.changeFields('Content-length',5);
class(r1.Header)
ans = 
'matlab.net.http.HeaderField'

Version History

Introduced in R2016b