http.isValidHeaderValue(value[, options])


如果 value 是一个有效的 HTTP 头部值,则返回 true,否则返回 false。默认选项下,这与 http.validateHeaderValue() 执行的检查相同,只是返回结果而不是抛出错误。

🌐 Returns true if value is a valid HTTP header value, and false otherwise. With the default options this is the same check that http.validateHeaderValue() performs, but the result is returned instead of an error being thrown.

undefined 和符号从来都不是有效的头部值。其他非字符串的值会在检查之前被转换成字符串,就像它们传给 outgoingMessage.setHeader(name, value) 时一样。

传入无效的 options 参数会抛出错误。

🌐 Passing an invalid options argument throws.

import { isValidHeaderValue } from 'node:http';

console.log(isValidHeaderValue('text/html')); // true
console.log(isValidHeaderValue(123)); // true
console.log(isValidHeaderValue(undefined)); // false
console.log(isValidHeaderValue('a\r\nb')); // false
console.log(isValidHeaderValue('a\x01b')); // false
console.log(isValidHeaderValue('a\x01b', { httpValidation: 'relaxed' })); // trueconst { isValidHeaderValue } = require('node:http');

console.log(isValidHeaderValue('text/html')); // true
console.log(isValidHeaderValue(123)); // true
console.log(isValidHeaderValue(undefined)); // false
console.log(isValidHeaderValue('a\r\nb')); // false
console.log(isValidHeaderValue('a\x01b')); // false
console.log(isValidHeaderValue('a\x01b', { httpValidation: 'relaxed' })); // true