步骤发送HTTPS请求在节点JS休息服务(Steps to send a https request

2019-07-03 10:40发布

什么是送在节点的JS HTTPS请求休息服务的步骤? 我有一个API暴露喜欢https://133-70-97-54-43.sample.com/feedSample/Query_Status_View/Query_Status/Output1?STATUS=Joined%20school

如何传递请求,什么是我需要给像主机,端口,路径和方法,这个API的选项?

Answer 1:

最简单的方法是使用请求模块。

request('https://example.com/url?a=b', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
});


Answer 2:

只是使用核心HTTPS与模块https.request功能。 例如,对于一个POST请求( GET将是相似的):

var https = require('https');

var options = {
  host: 'www.google.com',
  port: 443,
  path: '/upload',
  method: 'POST'
};

var req = https.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();


Answer 3:

使用请求模块解决了问题。

// Include the request library for Node.js   
var request = require('request');
//  Basic Authentication credentials   
var username = "vinod"; 
var password = "12345";
var authenticationHeader = "Basic " + new Buffer(username + ":" + password).toString("base64");
request(   
{
url : "https://133-70-97-54-43.sample.com/feedSample/Query_Status_View/Query_Status/Output1?STATUS=Joined%20school",
headers : { "Authorization" : authenticationHeader }  
},
 function (error, response, body) {
 console.log(body); }  );         


Answer 4:

请注意,如果您使用https.request不直接用身体从res.on('data',.. 。如果你有一个大的数据块中的未来这将会失败。因此,你需要连接的所有数据,然后过程在响应res.on('end'为例。 -

  var options = {
    hostname: "www.google.com",
    port: 443,
    path: "/upload",
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(post_data)
    }
  };

  //change to http for local testing
  var req = https.request(options, function (res) {
    res.setEncoding('utf8');

    var body = '';

    res.on('data', function (chunk) {
      body = body + chunk;
    });

    res.on('end',function(){
      console.log("Body :" + body);
      if (res.statusCode != 200) {
        callback("Api call failed with response code " + res.statusCode);
      } else {
        callback(null);
      }
    });

  });

  req.on('error', function (e) {
    console.log("Error" : " + e.message);
    callback(e);
  });

  // write data to request body
  req.write(post_data);
  req.end();


Answer 5:

您可以使用的SuperAgent和节点的链接模块都建立一个请求,就像这样:

var request = require('superagent');
var url = require('url');

var urlObj = {
  protocol: 'https',
  host: '133-70-97-54-43.sample.com',
  pathname: '/feedSample/Query_Status_View/Query_Status/Output1'
};

request
  .get(url.format(urlObj))
  .query({'STATUS': 'Joined school'})
  .end(function(res) {
    if (res.ok) {
      console.log(res.body);
    }
  });


文章来源: Steps to send a https request to a rest service in Node js
标签: node.js rest