将https请求发送到Node js中的休息服务的步骤

vin*_*nod 28 rest node.js

将节点js中的https请求发送到休息服务的步骤是什么?我有一个api暴露像https://133-70-97-54-43.sample.com/feedSample/Query_Status_View/Query_Status/Output1?STATUS=Joined%20school

如何传递请求以及我需要为此API提供哪些选项,如主机,端口,路径和方法?

zem*_*rco 49

只需将核心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();
Run Code Online (Sandbox Code Playgroud)

  • 我完全一样,但套接字挂断错误。TLS 似乎失败了。 (3认同)

jos*_*736 33

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

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

  • 更新:`request` [已被弃用](https://github.com/request/request/issues/3142),Aniket 的回答是截至 2020 年 5 月最好的回答 (11认同)
  • @josh3736 根据文档和前进路径,它已被弃用。* 请求将停止接受新功能。* 请求将停止考虑重大更改。 (2认同)

Ani*_*kur 6

请注意,如果您使用的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();
Run Code Online (Sandbox Code Playgroud)

  • @ChitrankDixit 我不完全理解你的用例,但你可以这样做。看看 https://nodejs.org/api/https.html#https_https_request_options_callback。你可以做`https.request(url[, options][,callback])` (2认同)

vin*_*nod 5

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

// 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); }  );         
Run Code Online (Sandbox Code Playgroud)