Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 819
mike Avatar asked Aug 26 '13 17:08

mike


2 Answers

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.

like image 114
josh3736 Avatar answered Oct 30 '22 21:10

josh3736


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

like image 39
Chad Avatar answered Oct 30 '22 21:10

Chad