Sankalp
Sankalp

Reputation: 1350

HTTP Get Request from NodeJS

I am trying to create http get request from node, to get information from youtube URL. When I click it in browser I get json response but if I try it from node, I get ssl and other types of error. What I have done is,

this.getApiUrl(params.videoInfo, function (generatedUrl) {
// Here is generated URL - // https://www.googleapis.com/youtube/v3/videos?key=AIzaSyAm_1TROkfNgY-bBuHmSaletJhVQmkycJc&id=_H_r9qVrf24&part=id%2Csnippet%2CcontentDetails%2Cplayer%2Cstatistics%2Cstatus

    console.log(generatedUrl);
    var req = http.get(generatedUrl, function (response) {
        var str = '';
        console.log('Response is ' + response.statusCode);
        response.on('data', function (chunk) {
            str += chunk;
        });
        response.on('end', function () {
            console.log(str);
        });
    });
    req.end();
    req.on('error', function (e) {
        console.log(e);
    });
});

I get this error

{
  "error": {
    "message": "Protocol \"https:\" not supported. Expected \"http:\".",
    "error": {}
  }
}

When I make it without https I get this error,

Response is 403

{"error":{"errors":[{"domain":"global","reason":"sslRequired","message":"SSL is required to perform this operation."}],"code":403,"message":"SSL is required to perform this operation."}}

Upvotes: 2

Views: 3838

Answers (2)

Raf
Raf

Reputation: 7649

Your problem is obviously accessing content served securely with http request hence, the error. As I have commented in your question, you can make use of https rather than http and that should work but, you can also use any of the following approaches.

Using request module as follow:

var url = "https://www.googleapis.com/youtube/v3/videos?key=AIzaSyAm_1TROkfNgY-bBuHmSaletJhVQmkycJc&id=_H_r9qVrf24&part=id%2Csnippet%2CcontentDetails%2Cplayer%2Cstatistics%2Cstatus";

request(url, function (error, response, body) {
  if (!error && response.statusCode == 200) {
      console.log(body);
  }
});

Using https module you can do like below:

var https = require('https');

 var options = {
        hostname: 'www.googleapis.com', //your hostname youtu
        port: 443,
        path: '//youtube/v3/videos?key=AIzaSyAm_1TROkfNgY-bBuHmSaletJhVQmkycJc&id=_H_r9qVrf24&part=id%2Csnippet%2CcontentDetails%2Cplayer%2Cstatistics%2Cstatus',
        method: 'GET'
 };

  //or https.get() can also be used if not specified in options object
  var req = https.request(options, function(res) {
    console.log("statusCode: ", res.statusCode);
    console.log("headers: ", res.headers);

    res.on('data', function(d) {
      process.stdout.write(d);
    });
  });
  req.end();

  req.on('error', function(e) {
    console.error(e);
  });

You can also use requestify module and

  var url = "https://www.googleapis.com/youtube/v3/videos?key=AIzaSyAm_1TROkfNgY-bBuHmSaletJhVQmkycJc&id=_H_r9qVrf24&part=id%2Csnippet%2CcontentDetails%2Cplayer%2Cstatistics%2Cstatus";
      requestify.get(url).then(function(response) {
          // Get the response body
          console.log(response.body);
      });

superagent module is another option

var url = "https://www.googleapis.com/youtube/v3/videos?key=AIzaSyAm_1TROkfNgY-bBuHmSaletJhVQmkycJc&id=_H_r9qVrf24&part=id%2Csnippet%2CcontentDetails%2Cplayer%2Cstatistics%2Cstatus";
  superagent('GET', url).end(function(response){
    console.log('Response text:', response.body);
});

Last but not least is the unirest module allow you to make http/https request as simple as follow:

var url = "https://www.googleapis.com/youtube/v3/videos?key=AIzaSyAm_1TROkfNgY-bBuHmSaletJhVQmkycJc&id=_H_r9qVrf24&part=id%2Csnippet%2CcontentDetails%2Cplayer%2Cstatistics%2Cstatus";
  unirest.get(url).end(function(res) {
    console.log(res.raw_body);
  });

There might be more options out there. Obviously you need to load the modules using require before using it

var request = require('request');
var https = require('https');
var requestify = require('requestify');
var superagent = require('superagent');
var unirest = require('unirest');

I provided extra details, not only to answer the question but, also to help others who browse for similiar question on how to make http/https request in nodejs.

Upvotes: 3

simon-p-r
simon-p-r

Reputation: 3751

You need to use the https module as opposed to the http module from node, also I would suggest one of many http libraries that provide a higher level api such as wreck or restler which allow you to control the protocol via options as opposed to a different required module.

Upvotes: 3

Related Questions