Node.js http.validateHeaderValue() Method
Last Updated :
06 Apr, 2023
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
const http = require( 'http' );
const { validateHeaderValue } = require( 'http' );
try {
validateHeaderValue( 'x-my-header' , undefined);
} catch (err) {
err instanceof TypeError;
console.log(& quot;Is undefined Invalid:& quot;,
err.code === 'ERR_HTTP_INVALID_HEADER_VALUE' );
console.log(err.message);
}
try {
http.validateHeaderValue( 'x-my-header' , 'oʊmɪɡə' );
} catch (err) {
err instanceof TypeError;
console.log(& quot;Is undefined Invalid:& quot;,
err.code === 'ERR_INVALID_CHAR' );
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
const http = require( 'http' );
const { validateHeaderName } = require( 'http' );
const PORT = process.env.PORT || 3000;
const httpServer = http.createServer(
function (request, response) {
response.setHeader( 'Content-Type' , 'text/html' );
response.setHeader( 'Set-Cookie' ,
[ 'type=ninja' , 'language=javascript' ]);
try {
validateHeaderValue( 'Content-Type' , 'text/html' );
} catch (err) {
console.log( "Error: " , err instanceof TypeError);
console.log(err.message);
}
try {
http.validateHeaderValue( 'Set-Cookie' , [ 'type=ninja' ,
'language=javascript' ]);
} catch (err) {
console.log( "Error: " , err instanceof TypeError);
console.log(err.message);
}
const headers = response.getHeaders();
console.log(headers);
response.end( 'ok' );
});
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
Please Login to comment...