Getting HTTP headers with node.js

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

something like,

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

will 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. 
+60
May 7 '11 at 17:34
source share
7 answers

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(); 
+122
May 14 '11 at 11:23
source

Try looking at http.get and the 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); }); 
+22
May 7 '11 at 18:08
source

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 want, but with HEAD you will get rid of getting the whole response body if you want to look at the headers.

+18
Feb 26 '14 at 5:52
source

I had problems with http.get ; so I switched to 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); } } ); 
+8
01 Oct '14 at 13:34 on
source

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

HEAD

requests a response identical to the one corresponding to the GET request, but without the response body. This is useful for retrieving the meta-information written in the response headers without transferring all the content.




Something like this, based 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); }); }); }); 
+7
May 7 '11 at 17:43
source

Here is my contribution regarding 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) }) 
+5
Apr 26 '17 at 10:55
source

For debugging the https protocol, I recommend: https://mitmproxy.org/

0
Feb 07 '19 at 13:38
source



All Articles