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()- 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
getmethod 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.Thegetmethod 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 ismq.{type: 'conversion-type'}
- Without a named header,
- 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_mqmdvariable. 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_mqmdvariable.var xml_mqmd = headers.get('MQMD');The return is as follows.<MQMD><Version>1</Version><Format>MQSTR</Format>...</MQMD> - Get the HTTP
Content-Typeheader and assign in to thecontentTypevariable. As you see, headers are not case-sensitive.var contentType = headers.get('content-type');
- Get the structured MQMD headers and assign it to the
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)- 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
setmethod associates a name to a value in a header. If the header exists, its value is replaced.The
setmethod is not used for original headers, which are read-only.Thesetmethod 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
setmethod 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 ismq.{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-Typeheader totext/xml.headers.set('Content-Type', 'text/xml'); - Set two noncoalesced
Set-Cookieheaders.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_mgmdvariable.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_mqmdvariable to an unstructured value.var json_mqmd = { MQMD: { "Version" : { "$" : "1" }, "Format" : { "$" : "MQSTR" } } }; headers.set({type: 'mq'}, 'MQMD', json_mqmd);
- Set the header with the raw value in the
- Set the
headers.types()
Returns an array of supported structured types for conversion.
- Syntax
headers.types()- Guidelines
- The
typesmethod returns an array of supported structured types for conversion. The only supported conversion type ismq. - Example
- Get the supported conversion types from the
originalheader.var hm = require('header-metadata'); var headers = hm.original; headers.types();