📜  Node.js http.ClientRequest.setHeader() 方法

📅  最后修改于: 2022-05-13 01:56:30.630000             🧑  作者: Mango

Node.js http.ClientRequest.setHeader() 方法

http.ClientRequest.setHeader()是 HTTP 模块中 ClientRequest 类的内置应用程序编程接口,用于设置标头对象。

句法:

const request.setHeader(name, value)

参数:此方法将特定标头的名称和值作为键值对的参数。

返回值:此方法没有任何返回值。

示例 1:文件名-index.js

Javascript
// Node.js program to demonstrate the  
// request.setHeader() 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',
  });
  
  req.setHeader('content-type', 'text/html');
  
  console.log("before operation :- " 
    + req.getHeader('content-type'))
  
  // Removing header by using
  // removeHeader() method
  req.removeHeader('content-type')
  
  console.log("after operation :- " 
  + req.getHeader('content-type'))
  
  process.exit(0)
});


Javascript
// Node.js program to demonstrate the  
// request.setHeader() 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.setHeader('Cookie', ['type=ninja',
            'language=javascript']);
  
  console.log("before operation :- " 
  + req.getHeader('Cookie'))
  
  // Removing header by using
  // removeHeader() method
  req.removeHeader('Cookie')
  
  console.log("after operation :- " 
  + req.getHeader('Cookie'))
  
  process.exit(0)
});


使用以下命令运行index.js文件:

node index.js

输出:

before operation :- text/html
after operation :- undefined

示例 2:文件名-index.js

Javascript

// Node.js program to demonstrate the  
// request.setHeader() 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.setHeader('Cookie', ['type=ninja',
            'language=javascript']);
  
  console.log("before operation :- " 
  + req.getHeader('Cookie'))
  
  // Removing header by using
  // removeHeader() method
  req.removeHeader('Cookie')
  
  console.log("after operation :- " 
  + req.getHeader('Cookie'))
  
  process.exit(0)
});

使用以下命令运行index.js文件:

node index.js

输出:

before operation :- type=ninja,language=javascript
after operation :- undefined

参考:https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_request_setheader_name_value