NodeJs: HTTP Request – Response to REST API

Sending HTTP GET/POST Request and Response using Node JS.

Code:

var https = require("https");

exports.handler = (events,context,callback) => {
 
 var options = {
   host  :  'hostname',
   port  :  '443',
   path  : 'ifany',
   
   headers: {
    
    'Authorization' : 'Basic' + new Buffer("username" + ':' + "password").toString('base64')
   }
 };
 
 // request
 
 request = https.get(options, function(res){
 
  
  var body = "";
  
  res.on('data', function(data){
   body = body+data;
   
  });
  
  res.on('end', function(){
   
   // response
   
   Response = JSON.parse(body);
   
   console.log(Resposne);
      
  });
  
  callback(null,{
   
   "dialogAction":{
    
    "type": "close",
    "fullfillmentState":"Fullfilled",
    "message":{
     "contentType":"PlainText",
     "content" : "Thank You! A email has been sent."
    }
   }
  });
 });
 
};
Advertisements

One thought on “NodeJs: HTTP Request – Response to REST API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s