This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

changeFields

Class: matlab.net.http.Message
Package: matlab.net.http

Change existing fields in message header

Syntax

msg = changeFields(msg,fields)
msg = changeFields(msg,FieldName1,FieldValue1,...,FieldNameN,FieldValueN)

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'

Introduced in R2016b