Headers

Case-insensitive header names, append versus set, and Node.js versus browser forbidden-header rules, verified in Node.js 22.23.3.

モジュールページの本文は現在英語です。

The Headers interface stores HTTP metadata. Names are case-insensitive. append adds a value; get then returns the combined values separated by ", " for an ordinary header. set replaces the previous values for that name.

Examples were checked in Node.js 22.23.3 and do not send a request. In browsers, a forbidden request header such as Cookie or Host cannot be set on a fetch request. In this Node runtime, new Headers() and new Request() accepted those names. That browser restriction was not executed here.

Examples were checked in Node.js 22.23.3, which provides these Web APIs as built-in globals. This is not a browser DOM test, and the examples do not send network requests. See the MDN documentation for the complete API, including browser support.

Web APIs overview → · JavaScript tutorial and built-in APIs →

API index

APISummary
Headers.get(), append(), set(), has(), delete()Read and write headers with case-insensitive names, merging values with append and replacing them with set.
Headers.set(), getSetCookie(), invalid namesShow that Node.js accepted Cookie and Host on Headers and Request, and that an invalid name throws TypeError.

Case, append, and set

get("content-type"), get("X-A"), and has("CONTENT-TYPE") all succeed against names that were inserted with different casing. The iterator yields lowercase names.

append("X-A", "2") after an initial "1" makes get("x-a") return "1, 2". set("X-A", "only") replaces that combined value. delete("x-b") removes the header.

Headers.get(), append(), set(), has(), delete()

new Headers(init)
Headers.prototype.get(name)
Headers.prototype.append(name, value)
Headers.prototype.set(name, value)
Headers.prototype.has(name)
Headers.prototype.delete(name)
Headers.prototype.entries()

Read and write headers with case-insensitive names, merging values with append and replacing them with set.

ParameterDescription
initAn object, an array of pairs, or another Headers object.
name, valueThe header name and value. Names are matched case-insensitively.

Returns: get returns a string or null. has returns a boolean. entries yields lowercase name/value pairs.

javascript
const headers = new Headers({ "Content-Type": "text/plain", "X-A": "1" });
headers.append("X-A", "2");
headers.set("x-b", "B");
console.log(headers.get("content-type"));
console.log(headers.get("X-A"));
console.log(headers.get("x-a"));
console.log(headers.has("CONTENT-TYPE"));
console.log(JSON.stringify([...headers.entries()]));
headers.set("X-A", "only");
console.log(headers.get("x-a"));
headers.delete("x-b");
console.log(headers.has("x-b"));

Expected output

text/plain
1, 2
1, 2
true
[["content-type","text/plain"],["x-a","1, 2"],["x-b","B"]]
only
false

Related: Request and Response

MDN documentation →

Cookies, Host, and invalid names

In this Node.js 22.23.3 runtime, headers.set("Cookie", "sid=1") and set("Host", "evil.example") both succeed, and a new Request also keeps those values. That is not the browser fetch rule: browsers treat Cookie, Host, and several other names as forbidden request headers and refuse to set them on a request from frontend script.

Set-Cookie is special: append can store two cookies, get("set-cookie") returns them joined with ", ", and getSetCookie() returns the array ["a=1", "b=2"]. An invalid header name such as "Inva lid" throws TypeError. No request is sent, and cookie storage in a browser was not measured.

Headers.set(), getSetCookie(), invalid names

Headers.prototype.set(name, value)
Headers.prototype.getSetCookie()
new Request(input, { headers })

Show that Node.js accepted Cookie and Host on Headers and Request, and that an invalid name throws TypeError.

Throws

  • TypeError — Thrown for an invalid header name such as "Inva lid". Measured in Node.js 22.23.3.
  • Do not assume a browser will let page script set Cookie or Host. That restriction is a browser fetch rule and was not run in this Node example.
javascript
const headers = new Headers();
headers.set("Cookie", "sid=1");
headers.set("Host", "evil.example");
headers.append("Set-Cookie", "a=1");
headers.append("Set-Cookie", "b=2");
console.log(headers.get("cookie"));
console.log(headers.get("host"));
console.log(headers.get("set-cookie"));
console.log(JSON.stringify(headers.getSetCookie()));

const request = new Request("https://example.com/", {
  headers: { Cookie: "sid=1", Host: "evil.example", "X-Ok": "1" },
});
console.log(request.headers.get("cookie"), request.headers.get("host"), request.headers.get("x-ok"));

try {
  new Headers({ "Inva lid": "1" });
} catch (error) {
  console.log(error.name);
}

Expected output

sid=1
evil.example
a=1, b=2
["a=1","b=2"]
sid=1 evil.example 1
TypeError

MDN documentation →