On this page

    M

    http.isValidHeaderValue

    History
    http.isValidHeaderValue(value, options?): boolean
    Attributes
    value:any
    options:Object
    httpValidation?:string
    Validation strictness, one of 'strict' or 'relaxed'. These have the same meaning as the httpValidation option of http.createServer() and http.request(). Default: 'strict'.
    Returns:boolean

    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 and symbols are never valid header values. Other non-string values are converted to strings before being checked, as they are when passed to outgoingMessage.setHeader(name, value).

    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' })); // true
    const { 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