Steps to send a https request to a rest service in Node js

后端 未结 4 925
粉色の甜心
粉色の甜心 2020-12-13 09:24

What are the steps to send a https request in node js to a rest service? I have an api exposed like https://133-70-97-54-43.sample.com/feedSample/Query_Status_View/Query_Sta

相关标签:
4条回答
  • 2020-12-13 09:53

    Using the request module solved the issue.

    // 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); }  );         
    
    0 讨论(0)
  • 2020-12-13 10:02

    The easiest way is to use the request module.

    request('https://example.com/url?a=b', function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(body);
      }
    });
    
    0 讨论(0)
  • 2020-12-13 10:10

    just use the core https module with the https.request function. Example for a POST request (GET would be similar):

    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();
    
    0 讨论(0)
  • 2020-12-13 10:12

    Note if you are using https.request do not directly use the body from res.on('data',... This will fail if you have a large data coming in chunks. So you need to concatenate all the data and then process the response in res.on('end'. Example -

      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();
    
    0 讨论(0)
提交回复
热议问题