How to make remote REST call inside Node.js? any CURL?

murvinlai picture murvinlai · Apr 13, 2011 · Viewed 405k times · Source

In Node.js, other than using child process to make CURL call, is there a way to make CURL call to remote server REST API and get the return data?

I also need to set up the request header to the remote REST call, and also query string as well in GET (or POST).

I find this one: http://blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs

but it doesn't show any way to POST query string.

Answer

Raynos picture Raynos · Apr 13, 2011

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();