HTTP从NodeJS获取请求

时间:2015-11-20 10:01:14

标签: node.js

我正在尝试从节点创建http get请求,以从youtube URL获取信息。当我在浏览器中单击它时,我得到json响应,但如果我从节点尝试它,我会得到ssl和其他类型的错误。我所做的是,

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);
    });
});

我收到此错误

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

当我没有https时,我收到此错误,

回复是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."}}

2 个答案:

答案 0 :(得分:3)

您需要使用https模块而不是来自节点的http模块,我还建议使用许多http库中的一个提供更高级别的api,例如wreck或restler,它们允许您控制协议通过选项而不是不同的必需模块。

答案 1 :(得分:3)

您的问题显然是通过http请求访问安全服务的内容,因此错误。正如我在您的问题中所评论的那样,您可以使用https而不是http,这应该可以使用,但是,您也可以使用以下任何一种方法。

使用 request 模块如下:

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);
  }
});

使用 https 模块,您可以执行以下操作:

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);
  });

您还可以使用 requestify 模块和

  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 模块是另一种选择

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);
});

最后但并非最不重要的是,unirest模块允许您像下面这样简单地发出http / https请求:

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);
  });

可能还有更多选择。显然你需要在使用之前使用require加载模块

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

我提供了额外的详细信息,不仅是为了回答问题,还帮助其他人浏览有关如何在nodejs中发出http / https请求的类似问题。