7

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

Following the instruction, I should be able to get the response header by calling response.headers[''], however, seems it doesn't work, when I try to call var contentType = response.headers['Content-Type'], the contentType is undefined. (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')
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)
    }

1 Answer 1

10

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.'

Sign up to request clarification or add additional context in comments.

1 Comment

The code says content-encoding and the question says content-type. Both work here so long as lowercase is used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.