Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js http.ClientRequest.protocol Method

  • Last Updated : 02 Sep, 2021

The http.ClientRequest.protocol is an inbuilt application programming interface of class ClientRequest within the HTTP module which is used to get the object of client request protocol.

Syntax:

const request.protocol

Parameters: This function does not accept any argument as a parameter.

Return Value: This method returns the object of the client request protocol.

Example 1: Filename-index.js

Javascript




// Node.js program to demonstrate the 
// request.protocol method
 
// Importing http module
const http = require('http');
 
// Create an HTTP server
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('okay');
});
 
// Now that server is running
server.listen(3000, '127.0.0.1', () => {
 
  // Make a request
  const options = {
    port: 3000,
    host: '127.0.0.1',
    headers: {
      'Connection': 'Upgrade',
      'Upgrade': 'websocket'
    }
  };
 
  // Getting client request
  const req = http.request(options);
 
  req.protocol = 'HTTP'
 
  // Getting protocol
  // by using protocol method
  const v = req.protocol;
 
  // Display the result
  console.log("protocol :- " + v)
 
  process.exit(0)
});

 

 

Run the index.js file using the following command:

 

node index.js

Output:

 

protocol :- HTTP

Example 2: Filename-index.js

 

Javascript




// Node.js program to demonstrate the 
// request.protocol method
 
// Importing http module
const http = require('http');
 
// Create an HTTP server
http.createServer((req, res) => { })
  .listen(3000, '127.0.0.1', () => {
 
  // Getting client request
  const req = http.request({
    port: 3000,
    host: '127.0.0.1',
  });
 
  // Getting protocol
  // by using protocol method
  console.log("protocol :- " + req.protocol)
 
  process.exit(0)
});

 

 

Run the index.js file using the following command:

 

node index.js

Output:

 

protocol :- undefined

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_request_protocol

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!