header class methods

The methods that are provided by the header class.

The header class provides the following class methods.

headers.get()

Returns a JSON object that contains headers and their values.

Syntax
headers.get()
headers.get(name)
headers.get(options,name)
Parameters
name
String. The name of the header to retrieve.
options
JSON object. The structured form to use for the conversion. If you do not specify a conversion type, the header value is in its raw form.
Guidelines
The get method returns a JSON object that contains headers and their values. If the requested header does not exist in the headers collection, the call returns undefined.
The get method has three forms: no header, generic, and options.
  • Without a named header, get(), returns all headers and values. For example, the returned JSON object can have the following form.
    {'Content-Type': 'text/xml', '...', '...'}
  • For the generic form, get(name), returns the header value as a string. For IBM MQ headers, the value is an XML string.
  • For the options form, get(options, name), returns the header value as a JSON object. This object is converted based on the schema in options. The format of options is as follows, where conversion-type is one of the supported conversion types. The only supported conversion type is mq.
    {type: 'conversion-type'}
Examples
The following examples assume the declaration of the following statements.
var hm = require('header-metadata');
var headers = hm.current;
  • Get the structured MQMD headers and assign it to the json_mqmd variable. Use this approach for any IBM MQ header. Other common headers to get independently are MQMP, MQOD, MQRFH, and MQRFH2.
    var json_mqmd = headers.get({type: 'mq'}, 'MQMD');
    The return is as follows.
    {
      MQMD: {
        "Version" : { "$" : "1" },
        "Format" : { "$" : "MQSTR" },
          ⋮
      }
    }
  • Get the generic MQMD header as a raw value, which is an XML string, and assign it to the xml_mqmd variable.
    var xml_mqmd = headers.get('MQMD');
    The return is as follows.
    <MQMD><Version>1</Version><Format>MQSTR</Format>...</MQMD>
  • Get the HTTP Content-Type header and assign in to the contentType variable. As you see, headers are not case-sensitive.
    var contentType = headers.get('content-type');

headers.headers

Get all the headers.

Syntax
headers.headers
Guidelines
Equivalent to headers.get().

headers.remove()

Deletes the header that is identified by name from the headers collection.

Syntax
headers.remove(name)
Parameters
name
String. The name of the header to remove from the headers collection.

headers.set()

Associates a name to a value in a header.

Syntax
headers.set(name,value)
headers.set(options,name,value)
Parameters
name
String. The name of the header to set.
value
Form dependent. The value to associate with header.
options
JSON object. The structured form to use for the conversion to an unstructured value.
Guidelines
The set method associates a name to a value in a header. If the header exists, its value is replaced.

The set method is not used for original headers, which are read-only.

The set method has two forms: generic and options.
  • For the generic form, the value can be in any format. The generic form has no return.
  • For the options form, the value is a JSON object that contains the structured form that is specified in options. This object is converted to an unstructured value and set to the header. The set method writes the raw form for a single header that is converted from the input value. The format of options is as follows, where conversion-type is one of the supported conversion types. The only supported conversion type is mq.
    {type: 'conversion-type'}
Examples
The following examples assume the declaration of the following statements.
var hm = require('header-metadata');
var headers = hm.current;
  • Set the Content-Type header to text/xml.
    headers.set('Content-Type', 'text/xml');
  • Set two noncoalesced Set-Cookie headers.
    header.set('Set-Cookie',['name1=value1', 'name2=value2']);
  • Set the MQMD header to the value of a predefined variable. The following two calls have the same effect and produce the same result.
    headers.set('MQMD', '<MQMD><Version>1</Version><Format>MQSTR</Format></MQMD>');
    • Set the header with the raw value in the xmlString_mgmd variable.
      var xmlString_mqmd = <MQMD><Version>1</Version><Format>MQSTR</Format></MQMD>;
      headers.set('MQMD', xmlString_mqmd);
    • Set the header through a transformation from a structured value in the json_mqmd variable to an unstructured value.
      var json_mqmd = {
          MQMD: {
              "Version" : { "$" : "1" },
              "Format" : { "$" : "MQSTR" }
          }
      };
      headers.set({type: 'mq'}, 'MQMD', json_mqmd);

headers.types()

Returns an array of supported structured types for conversion.

Syntax
headers.types()
Guidelines
The types method returns an array of supported structured types for conversion. The only supported conversion type is mq.
Example
Get the supported conversion types from the original header.
var hm = require('header-metadata');
var headers = hm.original;
headers.types();