mailme365 mailme365 - 1 year ago 121
Node.js Question

How to get response header in node.js

I'm working on a nodejs project, I use request module to submit restful request and get response. (here is the module link:

Following the instruction, I should be able to get the response header by calling

, however, seems it doesn't work, when I try to call
var contentType = response.headers['Content-Type']
, the
. (When I use postman, I could get Content-Type from the response). Anyone knows what's wrong?

This is the instruction from the site:

var request = require('request')
{ method: 'GET'
, uri: ''
, 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)

Answer Source

In node, the headers are accessed by using lowercased names, so using response.headers['content-encoding'] is correct.

Your code snippet currently works for me and displays 'server encoded the data as: gzip.'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download