http.validateHeaderName(name)


在调用 res.setHeader(name, value) 时对提供的 name 执行低层验证。

将非法值作为 name 传入将导致抛出 TypeError,由 code: 'ERR_INVALID_HTTP_TOKEN' 标识。

在将标头传给 HTTP 请求或响应之前,不必使用此方法。 HTTP 模块将自动验证此类标头。 示例:

示例:

const { validateHeaderName } = require('node:http');

try {
  validateHeaderName('');
} catch (err) {
  err instanceof TypeError; // --> true
  err.code; // --> 'ERR_INVALID_HTTP_TOKEN'
  err.message; // --> 'Header name must be a valid HTTP token [""]'
}

Performs the low-level validations on the provided name that are done when res.setHeader(name, value) is called.

Passing illegal value as name will result in a TypeError being thrown, identified by code: 'ERR_INVALID_HTTP_TOKEN'.

It is not necessary to use this method before passing headers to an HTTP request or response. The HTTP module will automatically validate such headers. Examples:

Example:

const { validateHeaderName } = require('node:http');

try {
  validateHeaderName('');
} catch (err) {
  err instanceof TypeError; // --> true
  err.code; // --> 'ERR_INVALID_HTTP_TOKEN'
  err.message; // --> 'Header name must be a valid HTTP token [""]'
}