如何在 node.js 中获取响应头

mai*_*365 3 node.js

我正在开发一个 nodejs 项目,我使用请求模块提交安静的请求并获得响应。(这里是模块链接:https : //github.com/request/request

按照说明,我应该能够通过调用来获取响应标头response.headers[''],但是,似乎它不起作用,当我尝试调用时var contentType = response.headers['Content-Type']contentTypeis undefined。(当我使用邮递员时,我可以从响应中获取 Content-Type)。有谁知道出了什么问题?

这是网站的说明:

var request = require('request')
request(
    { method: 'GET'
    , uri: 'http://www.google.com'
    , gzip: true
    }
  , function (error, response, body) {
      // body is the decompressed response body
      console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
      console.log('the decoded data is: ' + body)
    }
Run Code Online (Sandbox Code Playgroud)

msc*_*dex 7

在节点中,头是通过使用小写名称来访问的,所以使用response.headers['content-encoding']是正确的。

您的代码片段目前适用于我并显示“服务器将数据编码为:gzip”。