Tuesday, October 9, 2018

Http POST request in NodeJS

To make http POST request in NodeJS, below can be done

var https = reqiuire('https');

 function makePOSTRequest() {
   var postData = {'key' : 'value'}
   var postOptions = {'host' : 'testserver.com','port' : '8080','path' : '/testpath','method' : 'POST',
   headers : {
      'Content-Type' : 'application/json',
      'Content-Length' : Buffer.byteLength(postData);
    } }

   var post_req = https.request(postOptions,function(res) {
       res.setEncoding('utf8');
       res.on('data', function(chunk) {
          console.log('chunk is '+chunk)
       })

   })

}

references:
https://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request-in-node-js

No comments:

Post a Comment