Getting HTTP headers with Node.js

node.js

node.js Problem Overview


Is there a built in way to get the headers of a specific address via node.js?

something like,

var headers = getUrlHeaders("http://stackoverflow.com");

would return

HTTP/1.1 200 OK.
Cache-Control: public, max-age=60.
Content-Type: text/html; charset=utf-8.
Content-Encoding: gzip.
Expires: Sat, 07 May 2011 17:32:38 GMT.
Last-Modified: Sat, 07 May 2011 17:31:38 GMT.
Vary: *.
Date: Sat, 07 May 2011 17:31:37 GMT.
Content-Length: 32516.

node.js Solutions


Solution 1 - node.js

This sample code should work:

var http = require('http');
var options = {method: 'HEAD', host: 'stackoverflow.com', port: 80, path: '/'};
var req = http.request(options, function(res) {
    console.log(JSON.stringify(res.headers));
  }
);
req.end();

Solution 2 - node.js

Try to look at http.get and response headers.

var http = require("http");

var options = {
  host: 'stackoverflow.com',
  port: 80,
  path: '/'
};

http.get(options, function(res) {
  console.log("Got response: " + res.statusCode);
  
  for(var item in res.headers) {
	console.log(item + ": " + res.headers[item]);
  }
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

Solution 3 - node.js

Using the excellent request module:

var request = require('request');
  request("http://stackoverflow.com", {method: 'HEAD'}, function (err, res, body){
  console.log(res.headers);
});

You can change the method to GET if you wish, but using HEAD will save you from getting the entire response body if you only wish to look at the headers.

Solution 4 - node.js

Here is my contribution, that deals with any URL using http or https, and use Promises.

const http = require('http')
const https = require('https')
const url = require('url')

function getHeaders(myURL) {
  const parsedURL = url.parse(myURL)
  const options = {
    protocol: parsedURL.protocol,
    hostname: parsedURL.hostname,
    method: 'HEAD',
    path: parsedURL.path
  }
  let protocolHandler = (parsedURL.protocol === 'https:' ? https : http)

  return new Promise((resolve, reject) => {
    let req = protocolHandler.request(options, (res) => {
      resolve(res.headers)
    })
    req.on('error', (e) => {
      reject(e)
    })
    req.end()
  })
}

getHeaders(myURL).then((headers) => {
  console.log(headers)
})

Solution 5 - node.js

I'm not sure how you might do this with Node, but the general idea would be to send an HTTP HEAD request to the URL you're interested in.

> ### HEAD > Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.


Something like this, based it on this question:

var cli = require('cli');
var http = require('http');
var url = require('url');

cli.parse();

cli.main(function(args, opts) {
        this.debug(args[0]);

        var siteUrl = url.parse(args[0]);
        var site = http.createClient(80, siteUrl.host);
        console.log(siteUrl);

        var request = site.request('HEAD', siteUrl.pathname, {'host' : siteUrl.host})
        request.end();

        request.on('response', function(response) {
                response.setEncoding('utf8');
                console.log('STATUS: ' + response.statusCode);
                response.on('data', function(chunk) {
                        console.log("DATA: " + chunk);
                });
        });
});

Solution 6 - node.js

I had some problems with http.get; so I switched to the lib request:

var request = require('request');

var url = 'http://blog.mynotiz.de/';

var options = {
	url: url,
	method: 'HEAD'
};

request(options, function (error, response, body) {
		if (error) {
			return console.error('upload failed:', error);
		}
		
		if (response.headers['content-length']) {
			var file_size = response.headers['content-length'];
			console.log(file_size);
		}
	}
);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlostsourceView Question on Stackoverflow
Solution 1 - node.jscleeView Answer on Stackoverflow
Solution 2 - node.jsyojimbo87View Answer on Stackoverflow
Solution 3 - node.jsNemoView Answer on Stackoverflow
Solution 4 - node.jsDirty HenryView Answer on Stackoverflow
Solution 5 - node.jsMatt BallView Answer on Stackoverflow
Solution 6 - node.jsFrank RothView Answer on Stackoverflow