Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create HTTPS client in NodeJS

I've been having a heck of a time figuring out how to use Node.js (v0.3.8) to securely connect to an HTTP server. I have the following code:

var http = require("http");
var client = http.createClient(443, host, /* secure= */ true);
var request = client.request("GET", relativeUrl, { host: host });

When I run it, I get:

node.js:116
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: Parse Error
    at Client.onData [as ondata] (http.js:1287:27)
    at Client._onReadable (net.js:648:27)
    at IOWatcher.onReadable [as callback] (net.js:156:10)

I've been Googling for answers for the past half hour, and have read the documentation at http://nodejs.org/ . What am I missing?

like image 818
Ryan Tenney Avatar asked Feb 09 '11 02:02

Ryan Tenney


2 Answers

It turns out I was using an old version of the Node documentation, which didn't include a reference to the https module. Referring to the current docs, I found http://nodejs.org/docs/latest/api/https.html#https_https_get_options_callback, which provides an example:

https.get({ host: 'encrypted.google.com', path: '/' }, function (res) { … });
like image 52
Ryan Tenney Avatar answered Oct 31 '22 13:10

Ryan Tenney


If you are using node.js as a client you should be able to simply substitute http for https.

That is according to the following website https://github.com/danwrong/Restler/

"Transparently handle SSL (just specify https in the URL)"
like image 40
Nick Avatar answered Oct 31 '22 15:10

Nick