{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":761,"path":"api-reference/server-api-reference/Fetch.HeadersAPI.md","title":"Fetch Headers - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# Fetch Headers- Scoped, Global\n\nThe Fetch Headers API allows you to retrieve, set, add to, and remove headers from a list of request or response headers.\n\nThis API is part of a set of Fetch APIs, which provide various actions for fetching resources from external websites. The full Fetch API collection includes:\n\n-   [Fetch - fetch\\(String resource, Object options\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/FetchAPI.md): Starts the process of fetching a resource from the network.\n-   [Fetch Headers - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.HeadersAPI.md): Retrieve and modify request and response headers.\n-   [Fetch Request - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.RequestAPI.md): Create a new request object.\n-   [Fetch RequestInit - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.RequestInitAPI.md): Set options to configure a fetch request.\n-   [Fetch Response - Scoped,Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.ResponseAPI.md): Create a new response object.\n\nTo support fetch actions, the system property, `glide.hosts.allowlist`, allows controls over what hosts a fetch method can access. It applies to HTTP APIs like RestMessageV2 and those mentioned above. For more information about `glide.hosts.allowlist`, see [Available system properties](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/r_AvailableSystemProperties.md).\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## Fetch Headers - Headers\\(init\\)\n\nThe Headers\\(\\) constructor creates a new, empty Headers object. You can also create and pre-populate a new Headers object with list of HTTP headers using the **init** parameter.\n\n**Note:** To add headers to an existing Headers object, use [Fetch Headers - append\\(String name, String value\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.HeadersAPI.md).\n\n<table id=\"table_nzn_3rv_gdc\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\ninit\n\n</td><td>\n\nObject\n\n</td><td>\n\nOptional. An object literal containing HTTP headers to pre-populate the new Headers object with. Valid values:\n\n-   An object literal with string values,\n-   An array of name-value pairs,\n-   An existing Headers object \\(where the new object copies data from the existing object\\).\n\nDefault: Creates an empty Headers object\n\n</td></tr></tbody>\n</table>The following example shows how to use the Headers\\(\\) constructor to create a new Headers object with and without headers.\n\n```\n//Intializes an empty Headers object.\nvar headers = new Headers();\n\n//Sets the new Headers object with a list of headers.\nvar httpHeaders = {\n  \"Content-Type\": \"application/json\", \n  \"X-My-Custom-Header\": \"XYZ\", \n}; \n\nvar myHeaders = new Headers(httpHeaders);\n```\n\n## Fetch Headers - append\\(String name, String value\\)\n\nAdds a new value to an existing header inside a Headers object, or adds the header if it does not already exist.\n\nIf the specified header already exists, `append()` changes its existing value to the specified value. If the specified header already exists and accepts multiple values, `append()` appends the new value to the end of the value set. To overwrite the old value with a new one, use [Fetch Headers - set\\(String name, String value\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.HeadersAPI.md).\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|Required. The name of the HTTP header to add to the Headers object.|\n|value|String|Required. The value of the HTTP header to add.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to add a header using append\\(\\):\n\n```\nvar myHeaders = new Headers(); // Currently empty\nmyHeaders.append(\"Content-Type\", \"image/jpeg\");\nmyHeaders.get(\"Content-Type\"); // Returns 'image/jpeg'\n```\n\nOutput:\n\n```\n“image/jpeg”\n```\n\nThe following example shows how to append a new header that accepts multiple values:\n\n```\nvar myHeaders = new Headers(); // Currently empty\nmyHeaders.append(\"Accept-Encoding\", \"deflate\");\nmyHeaders.append(\"Accept-Encoding\", \"gzip\");\nmyHeaders.get(\"Accept-Encoding\"); // Returns 'deflate, gzip'\n```\n\nOutput:\n\n```\n{“deflate”, “gzip”} \n```\n\n## Fetch Headers - delete\\(String name\\)\n\nDeletes a header from the current Headers object.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|The name of the HTTP header to delete from the Headers object.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to first add a header to the Headers object using append\\(\\), and then delete the same header using delete\\(\\).\n\n```\nvar myHeaders =newHeaders();// Currently empty\nmyHeaders.append(\"Content-Type\",\"image/jpeg\");\nmyHeaders.get(\"Content-Type\");// Returns 'image/jpeg'\n```\n\nThe following example shows that the endpoint returns a value of null if the Headers object is already deleted:\n\n```\nmyHeaders.delete(\"Content-Type\");\nmyHeaders.get(\"Content-Type\");// Returns null, as it has been deleted\n```\n\nOutput:\n\n```\nnull\n```\n\n## Fetch Headers - entries\\(\\)\n\nReturns an iterator allowing you to cycle through all key-value pairs \\(represented with strings\\) contained in the Headers object.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Iterator|Returns an iterator object, where both the key and value pairs are String objects.|\n\nThe following example shows how to create a test Headers object and then use the entries\\(\\) method to return an iterator of all values in the Headers object.\n\n```\n// Create a test Headers object\nvar myHeaders = new Headers();\nmyHeaders.append(\"Content-Type\", \"text/xml\");\nmyHeaders.append(\"Vary\", \"Accept-Language\");\n\n// Display the key/value pairs\nfor (var pair of myHeaders.entries()) {\n  console.log(`${pair[0]}: ${pair[1]}`);\n}\n```\n\nOutput:\n\n```\ncontent-type: text/xml\nvary: Accept-Language\n```\n\n## Fetch Headers - forEach\\(Function callbackFn, Object thisArg\\)\n\nExecutes a callback function once per each key/value pair in the Headers object.\n\nThe callback function does not execute for deleted keys but does for existing keys even if the value is undefined.\n\n<table id=\"table_pjv_xcq_jdc\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\ncallbackFn\n\n</td><td>\n\nFunction\n\n</td><td>\n\nFunction to execute for each key/value pair in the Headers object. Conforms to the format: `callbackFn(string key, string value)`.\n\n</td></tr><tr><td>\n\ncallbackFn.value\n\n</td><td>\n\nString\n\n</td><td>\n\nValue of the header entry.\n\n</td></tr><tr><td>\n\ncallbackFn.key\n\n</td><td>\n\nString\n\n</td><td>\n\nName of the header entry.\n\n</td></tr><tr><td>\n\ncallBackFn.object\n\n</td><td>\n\nHeader\n\n</td><td>\n\nThe Headers object to iterate.\n\n</td></tr><tr><td>\n\nthisArg\n\n</td><td>\n\nObject\n\n</td><td>\n\nValue to use as `this` when executing `callback`.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example logs a line for each key/value pair in the Headers object:\n\n```\n// Create a new test Headers object\nvar myHeaders = new Headers();\nmyHeaders.append(\"Content-Type\", \"application/json\");\nmyHeaders.append(\"Cookie\", \"This is a demo cookie\");\nmyHeaders.append(\"compression\", \"gzip\");\n\n// Display the key/value pairs\nmyHeaders.forEach((value, key) => {\n  console.log(`${key} ==> ${value}`);\n});\n```\n\nOutput:\n\n```\ncompression ==> gzip\ncontent-type ==> application/json\ncookie ==> This is a demo cookie\n```\n\n## Fetch Headers - get\\(String name\\)\n\nReturns a string of all header values of a specified Headers object, or returns null if the header does not exist.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|The name of the HTTP header whose headers values you want to retrieve \\(case-insensitive\\).|\n\n<table id=\"table_mg2_kfq_jdc\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nString\n\n</td><td>\n\nThe value of the retrieved header \\(represented as a string\\), or null if the header is not set.Returns an error if the header does not exist. Returns a type error if the given name is not that of an HTTP header.\n\n</td></tr></tbody>\n</table>The following shows how to create a headers object with multiple headers using append\\(\\), and then return the header's values using the get\\(\\) method.\n\n```\nvar myHeaders = new Headers(); // Currently empty\nmyHeaders.append(\"Accept-Encoding\", \"deflate\");\nmyHeaders.append(\"Accept-Encoding\", \"gzip\");\nmyHeaders.get(\"Accept-Encoding\"); // Returns \"deflate, gzip\"\nmyHeaders\n  .get(\"Accept-Encoding\")\n  .split(\",\")\n  .map((v) => v.trimStart()); // Returns [ \"deflate\", \"gzip\"\n\n```\n\nOutput:\n\n```\n[ \"deflate\", \"gzip\" ]\n```\n\n## Fetch Headers - getSetCookie\\(\\)\n\nReturns all Set-Cookie headers associated with a response.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Array of strings|An array of strings containing the values of all Set-Cookie headers associated with a response. The method returns an empty array if no Set-Cookie headers are set.|\n\nThe following script example shows how to use getSetCookie\\(\\) to return the header values associated with the response:\n\n```\nvar headers = new Headers({\n  \"Set-Cookie\": \"name1=value1\",\n});\n\nheaders.append(\"Set-Cookie\", \"name2=value2\");\n\nheaders.getSetCookie();\n```\n\nOutput:\n\n```\n[\"name1=value1\", \"name2=value2\"]\n```\n\n## Fetch Headers - has\\(String name\\)\n\nReturns a Boolean indicating whether a Headers object contains the given header.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|The name of the HTTP header to check whether it exists on the Headers object or not.|\n\n<table id=\"table_cvr_r3q_jdc\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the specified header exists in the Headers object. A TypeError is returned if the given header is not a valid HTTP header name.Possible values:\n\n-   true: The header exists on the object.\n-   false: The header doesn't exist on the object.\n\n</td></tr></tbody>\n</table>The following example uses the Headers\\(\\) and append\\(\\) methods to create and set a Headers object with the Content-Type header. The example then uses the has\\(\\) method to show that the Content-Type header exists on the Headers object but a Accept-Coding header does not.\n\n```\nvar myHeaders = new Headers(); // Currently empty\nmyHeaders.append(\"Content-Type\", \"image/jpeg\");\nmyHeaders.has(\"Content-Type\"); // Returns true\nmyHeaders.has(\"Accept-Encoding\"); // Returns false\n```\n\nOutput:\n\n```\ntrue\nfalse\n```\n\n## Fetch Headers - keys\\(\\)\n\nReturns an iterator allowing you to cycle through all keys contained in a Headers object.\n\nUse the [Fetch Headers - values\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.HeadersAPI.md) method to return all header keys.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Iterator|Returns an iterator, where header keys are represented as Strings.|\n\nThe following example shows how to create a Headers object with keys, and then use the keys\\(\\) method to return a list of available headers.\n\n```\n// Create a test Headers object\nvar myHeaders = new Headers();\nmyHeaders.append(\"Content-Type\", \"text/xml\");\nmyHeaders.append(\"Vary\", \"Accept-Language\");\n\n// Display the keys\nfor (var key of myHeaders.keys()) {\n  console.log(key);\n}\n```\n\nOutput:\n\n```\ncontent-type\nvary\n```\n\n## Fetch Headers - set\\(String name, String value\\)\n\nUpdates a value of an existing header inside the Headers object, or creates a new header with the given value if the header does not already exist.\n\nTo simply append a new value to an existing header without overwriting any data, use the [Fetch Headers - append\\(String name, String value\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.HeadersAPI.md) method.\n\n|Name|Type|Description|\n|----|----|-----------|\n|name|String|The name of the HTTP header in which to set a new value. Throws TypeError if the given name is not the name of an HTTP header.|\n|value|String|The new value to set for the given header.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to set a header in new Headers object using append\\(\\), and then reset the value using set\\(\\).\n\n```\nvar myHeaders = new Headers(); // Currently empty\nmyHeaders.append(\"Content-Type\", \"image/jpeg\");\nmyHeaders.set(\"Content-Type\", \"text/html\");\n```\n\nOutput:\n\n```\ntext/html\n```\n\nThe following example shows how to set a value in a Headers object when it accepts multiple values:\n\n```\nmyHeaders.set(\"Accept-Encoding\", \"deflate\");\nmyHeaders.set(\"Accept-Encoding\", \"gzip\");\nmyHeaders.get(\"Accept-Encoding\");\n```\n\nOutput:\n\n```\ngzip\n```\n\n## Fetch Headers - values\\(\\)\n\nReturns an iterator allowing you to cycle through all header values contained in the Headers object.\n\nUse the [Fetch Headers - keys\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/Fetch.HeadersAPI.md) method to return all header values.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|Returns an iterator, where the header values are represented as strings.|\n\nThe following example shows how to fist create and set a new Headers object with multiple headers using the append\\(\\) method, and then return a list of all header values in the new object by calling the values\\(\\) method. Console.log\\(\\) logs the line to system logs.\n\n```\n// Create a test Headers object\nvar myHeaders = new Headers();\nmyHeaders.append(\"Content-Type\", \"text/xml\");\nmyHeaders.append(\"Vary\", \"Accept-Language\");\n\n// Display the values\nfor (var value of myHeaders.values()) {\n  console.log(value);\n}\n```\n\nOutput:\n\n```\ntext/xml\nAccept-Language\n```\n\n"}}