Open In App

Node.js http.validateHeaderValue() Method

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The http.validateHeaderValue() (Added in v14.3.0) property is an inbuilt property of the ‘http’ module which performs the low-level validations on the provided value that are done when res.setHeader(name, value) is called.

Passing an illegal value as the value will result in a TypeError being thrown. An undefined value error is identified by code: ‘ERR_HTTP_INVALID_HEADER_VALUE‘. Invalid value character error is identified by code: ‘ERR_INVALID_CHAR‘.

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.

Note: Use the latest version of Node.js to get the required Output.

In order to get a response and a proper result, we need to import ‘http’ module.

const http = require('http');

Syntax:

http.validateHeaderValue(name, value);

Parameters: This method accepts two parameters as mentioned above and described below:

  • name <String>: It accepts the name of the header and it is case-insensitive.
  • value <any>: It accepts any function, array, or string.

Return Value: It does not return any value, instead performs the low-level validations on the provided value.

The below example illustrates the use of http.validateHeaderValue() property in Node.js.

Example 1: Filename: index.js

Javascript




// Node.js program to demonstrate the
// http.validateHeaderValue() Method
 
// Importing http module
const http = require('http');
const { validateHeaderValue } = require('http');
 
// Handling Errors via try-catch
try {
    validateHeaderValue('x-my-header', undefined);
} catch (err) {
    err instanceof TypeError; // true
 
    // Printing Errors
    console.log(& quot;Is undefined Invalid:& quot;,
    err.code === 'ERR_HTTP_INVALID_HEADER_VALUE'); // true
 
    // 'Invalid value "undefined" for header "x-my-header"'
    console.log(err.message);
}
// Handling Errors via try-catch
try {
    http.validateHeaderValue('x-my-header', 'oÊŠmɪɡə');
} catch (err) {
    err instanceof TypeError; // --> true
 
    // Printing Errors
    console.log(& quot;Is undefined Invalid:& quot;,
    err.code === 'ERR_INVALID_CHAR'); // true
 
    // 'Invalid character in header content ["x-my-header"]'
    console.log(err.message);
}


Run the index.js file using the following command:

node index.js

Output:

Is undefined Invalid: true

Invalid value “undefined” for header “x-my-header”

Is undefined Invalid: true

Invalid character in header content [“x-my-header”]

Example 2: Filename: index.js

Javascript




// Node.js program to demonstrate the
// http.validateHeaderValue() Method
 
// Importing http module
const http = require('http');
 
// Another way to import
const { validateHeaderName } = require('http');
 
// Setting up PORT
const PORT = process.env.PORT || 3000;
 
// Creating http Server
const httpServer = http.createServer(
    function (request, response) {
 
        // Setting up Headers
        response.setHeader('Content-Type', 'text/html');
        response.setHeader('Set-Cookie',
            ['type=ninja', 'language=javascript']);
 
        // Handling Errors via try-catch
        try {
            validateHeaderValue('Content-Type', 'text/html');
        } catch (err) {
 
            // false
            console.log("Error: ", err instanceof TypeError);
 
            // Printing Errors
            console.log(err.message);
        }
 
        try {
            http.validateHeaderValue('Set-Cookie', ['type=ninja',
                'language=javascript']);
        } catch (err) {
 
            // false
            console.log("Error: ", err instanceof TypeError);
 
            // Printing Errors
            console.log(err.message);
        }
 
        // Getting the set Headers
        const headers = response.getHeaders();
 
        // Printing those headers
        console.log(headers);
 
        // Prints Output on the
        // browser in response
        response.end('ok');
    });
 
// Listening to http Server
httpServer.listen(PORT, () => {
    console.log("Server is running at port 3000...");
});


Run the index.js file using the following command:

node index.js

Output:

Output: In-Console

Server is running at port 3000…

Error: false

Valid header…

[Object: null prototype]{

‘Content-Type’: ‘text/html’,

‘Set-Cookie’: [‘type=ninja’, ‘language=javascript’]}

Now run http://localhost:3000/ in the browser.

Output: In-Browser

ok 
 

Reference: https://nodejs.org/api/http.html#http_http_validateheadervalue_name_value



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads