How to make remote REST call inside Node.js? any CURL?
Look at http.request
var options = { host: url, port: 80, path: '/resource?id=foo&bar=baz', method: 'POST'};http.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); });}).end();
How about using Request — Simplified HTTP client.
Edit February 2020: Request has been deprecated so you probably shouldn't use it any more.
Here's a GET:
var request = require('request');request('http://www.google.com', function (error, response, body) { if (!error && response.statusCode === 200) { console.log(body) // Print the google web page. }})
OP also wanted a POST:
request.post('http://service.com/upload', {form:{key:'value'}})
Look at http://isolasoftware.it/2012/05/28/call-rest-api-with-node-js/
var https = require('https');/** * HOW TO Make an HTTP Call - GET */// options for GETvar optionsget = { host : 'graph.facebook.com', // here only the domain name // (no http/https !) port : 443, path : '/youscada', // the rest of the url with parameters if needed method : 'GET' // do GET};console.info('Options prepared:');console.info(optionsget);console.info('Do the GET call');// do the GET requestvar reqGet = https.request(optionsget, function(res) { console.log("statusCode: ", res.statusCode); // uncomment it for header details // console.log("headers: ", res.headers); res.on('data', function(d) { console.info('GET result:\n'); process.stdout.write(d); console.info('\n\nCall completed'); });});reqGet.end();reqGet.on('error', function(e) { console.error(e);});/** * HOW TO Make an HTTP Call - POST */// do a POST request// create the JSON objectjsonObject = JSON.stringify({ "message" : "The web of things is approaching, let do some tests to be ready!", "name" : "Test message posted with node.js", "caption" : "Some tests with node.js", "link" : "http://www.youscada.com", "description" : "this is a description", "picture" : "http://youscada.com/wp-content/uploads/2012/05/logo2.png", "actions" : [ { "name" : "youSCADA", "link" : "http://www.youscada.com" } ]});// prepare the headervar postheaders = { 'Content-Type' : 'application/json', 'Content-Length' : Buffer.byteLength(jsonObject, 'utf8')};// the post optionsvar optionspost = { host : 'graph.facebook.com', port : 443, path : '/youscada/feed?access_token=your_api_key', method : 'POST', headers : postheaders};console.info('Options prepared:');console.info(optionspost);console.info('Do the POST call');// do the POST callvar reqPost = https.request(optionspost, function(res) { console.log("statusCode: ", res.statusCode); // uncomment it for header details// console.log("headers: ", res.headers); res.on('data', function(d) { console.info('POST result:\n'); process.stdout.write(d); console.info('\n\nPOST completed'); });});// write the json datareqPost.write(jsonObject);reqPost.end();reqPost.on('error', function(e) { console.error(e);});/** * Get Message - GET */// options for GETvar optionsgetmsg = { host : 'graph.facebook.com', // here only the domain name // (no http/https !) port : 443, path : '/youscada/feed?access_token=you_api_key', // the rest of the url with parameters if needed method : 'GET' // do GET};console.info('Options prepared:');console.info(optionsgetmsg);console.info('Do the GET call');// do the GET requestvar reqGet = https.request(optionsgetmsg, function(res) { console.log("statusCode: ", res.statusCode); // uncomment it for header details// console.log("headers: ", res.headers); res.on('data', function(d) { console.info('GET result after POST:\n'); process.stdout.write(d); console.info('\n\nCall completed'); });});reqGet.end();reqGet.on('error', function(e) { console.error(e);});