From db8736ad93231986b747e21131719fdacdf39a02 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 21 Jan 2011 13:21:01 -0800 Subject: [PATCH] Add https.get() --- doc/api/https.markdown | 21 ++++++++++++++++++++- lib/https.js | 10 ++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/doc/api/https.markdown b/doc/api/https.markdown index ac1edf04c3..b98c16b1f0 100644 --- a/doc/api/https.markdown +++ b/doc/api/https.markdown @@ -23,9 +23,10 @@ Example: }).listen(8000); -## https.request +## https.request(options, callback) Makes a request to a secure web server. +Similar options to `http.request()`. Example: @@ -52,7 +53,25 @@ Example: console.error(e); }); +## https.get(options, callback) +Like `http.get()` but for HTTPS. + +Example: + + var https = require('https'); + + https.get({ host: 'encrypted.google.com', path: '/' }, function(res) { + console.log("statusCode: ", res.statusCode); + console.log("headers: ", res.headers); + + res.on('data', function(d) { + process.stdout.write(d); + }); + + }).on('error', function(e) { + console.error(e); + }); diff --git a/lib/https.js b/lib/https.js index e2555f0017..aaa070a7aa 100644 --- a/lib/https.js +++ b/lib/https.js @@ -44,6 +44,8 @@ Agent.prototype._getConnection = function(host, port, cb) { function getAgent(options) { + if (!options.port) options.port = 443; + var id = options.host + ':' + options.port; var agent = agents[id]; @@ -59,3 +61,11 @@ exports.request = function(options, cb) { var agent = getAgent(options); return http._requestFromAgent(agent, options, cb); }; + + +exports.get = function(options, cb) { + options.method = 'GET'; + var req = exports.request(options, cb); + req.end(); + return req; +};