Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do this curl operation in node.js

What I would like to do is this curl operation in node.js.

curl -XPOST localhost:12060/repository/schema/fieldType -H 'Content-Type: application/json' -d '
{
  action: "create",
  fieldType: {
    name: "n$name",
    valueType: { primitive: "STRING" },
    scope: "versioned",
    namespaces: { "my.demo": "n" }
  }
}' -D -

Suggestions are appreciated.

like image 773
XMen Avatar asked Sep 21 '11 11:09

XMen


1 Answers

via here http://query7.com/nodejs-curl-tutorial

Although there are no specific NodeJS bindings for cURL, we can still issue cURL requests via the command line interface. NodeJS comes with the child_process module which easily allows us to start processes and read their output. Doing so is fairly straight forward. We just need to import the exec method from the child_process module and call it. The first parameter is the command we want to execute and the second is a callback function that accepts error, stdout, stderr.

var util = require('util');
var exec = require('child_process').exec;

var command = 'curl -sL -w "%{http_code} %{time_total}\\n" "http://query7.com" -o /dev/null'

child = exec(command, function(error, stdout, stderr){

console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);

if(error !== null)
{
    console.log('exec error: ' + error);
}

});

EDIT This is also a possible solution: https://github.com/dhruvbird/http-sync

like image 51
mrryanjohnston Avatar answered Sep 23 '22 00:09

mrryanjohnston