Node js Error: Protocol "https:" not supported. Expected "http:" Node js Error: Protocol "https:" not supported. Expected "http:" json json

Node js Error: Protocol "https:" not supported. Expected "http:"


When you want to request an https resource, you need to use https.get, not http.get.

https://nodejs.org/api/https.html


As a side note to anyone looking for a solution from Google... make sure you are not using an http.Agent with an https request or you will get this error.


The reason for this error is that you are trying to call a HTTPS URI from a HTTP client. The ideal solution would have been for a generic module to figure out the URI protocol and take the decision to use HTTPS or HTTP internally.

The way I overcame this problem is by using the switching logic on my own. Below is some code which did the switching for me.

    var http = require('http');    var https = require('https');    // Setting http to be the default client to retrieve the URI.    var url = new URL("https://www.google.com")    var client = http; /* default  client */    // You can use url.protocol as well     /*if (url.toString().indexOf("https") === 0){                client = https;    }*/    /* Enhancement : using the  URL.protocol  parameter     * the  URL  object ,  provides a  parameter url.protocol that gives you      * the protocol  value  ( determined  by the  protocol ID  before      * the ":" in the  url.      * This makes it easier to  determine the protocol, and to  support other       * protocols like ftp ,  file  etc)      */   client=(url.protocol=="https") ? https:client;     // Now the client is loaded with the correct Client to retrieve the URI.    var req = client.get(url, function(res){        // Do what you wanted to do with the response 'res'.        console.log(res);    });