Skip to content

Instantly share code, notes, and snippets.

@thlorenz
Last active December 20, 2015 01:08
Show Gist options
  • Save thlorenz/6046358 to your computer and use it in GitHub Desktop.
Save thlorenz/6046358 to your computer and use it in GitHub Desktop.
HEAD request using https and hyperquest
// The below works, although having to call res.on('data') is not ideal
var https = require('https');
var opts = {
hostname : 'github.com'
, path : '/substack/hyperquest'
, method : 'GET'
}
https.request(opts, function (res) {
console.log('statusCode: ', res.statusCode);
res.on('data', function () { /* don't care but need to call res.on('data', fn) since it hangs otherwise */ })
}).end();
// => statusCode: 200
// The below does the same as the above. Tricky part that you have to call .end() as well (not documented on hyperquest site).
// Advantage is that res.on('data') call is not needed since it returns without that call
var hyperquest = require('hyperquest');
var url = 'https://github.com/substack/hyperquest';
var opts = { method: 'HEAD' };
hyperquest(url, opts, function (err, res) {
if (err) return console.error(err);
console.log('statusCode: ', res.statusCode);
}).end();
// => statusCode: 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment