Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What are the steps to send a https request in node js to a rest service? I have an api exposed like (Original link not working...)

How to pass the request and what are the options I need to give for this API like host, port, path and method?

like image 900
vinod Avatar asked Oct 29 '12 12:10

vinod


People also ask

How do I use HTTP request in node JS?

Step to run the application: Open the terminal and write the following command. Approach 3 : Here we will send a request to updating a resource using node-fetch library. If you are already worked with Fetch in browser then it may be your good choice for your NodeJS server. Rewrite the index.

How do I make a HTTPS POST in node JS?

get function working here... const https = require("https"); function get(url, callback) { "use-strict"; https. get(url, function (result) { var dataQueue = ""; result. on("data", function (dataBuffer) { dataQueue += dataBuffer; }); result.

How do I send a node js server request?

Server (Node.js)var server = http. createServer(function (request, response) { var queryData = url. parse(request. url, true).


1 Answers

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(); 
like image 190
zemirco Avatar answered Sep 23 '22 15:09

zemirco