mike
mike

Reputation: 93

In node.js, how do I get the Content-Length header in response to http.get()?

I have the following script and it seems as though node is not including the Content-Length header in the response object. I need to know the length before consuming the data and since the data could be quite large, I'd rather not buffer it.

http.get('http://www.google.com', function(res){    

    console.log(res.headers['content-length']); // DOESN'T EXIST
});

I've navigated all over the object tree and don't see anything. All other headers are in the 'headers' field.

Any ideas?

Upvotes: 9

Views: 20418

Answers (2)

josh3736
josh3736

Reputation: 144912

www.google.com does not send a Content-Length. It uses chunked encoding, which you can tell by the Transfer-Encoding: chunked header.

If you want the size of the response body, listen to res's data events, and add the size of the received buffer to a counter variable. When end fires, you have the final size.

If you're worried about large responses, abort the request once your counter goes above how ever many bytes.

Upvotes: 10

Chad
Chad

Reputation: 19609

Not every server will send content-length headers.

For example:

http.get('http://www.google.com', function(res) {
    console.log(res.headers['content-length']); // undefined
});

But if you request SO:

http.get('http://stackoverflow.com/', function(res) {
    console.log(res.headers['content-length']); // 1192916
});

You are correctly pulling that header from the response, google just doesn't send it on their homepage (they use chunked encoding instead).

Upvotes: 5

Related Questions