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


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:

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

2/28/2013 12:53:49 AM

Accepted Answer

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.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
10/3/2015 6:19:22 PM

How about using Request — Simplified HTTP client.

Here's a GET:

var request = require('request');
request('', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body) // Print the google web page.

OP also wanted a POST:'', {form:{key:'value'}})

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow