0

Hello I want to use xively with the curl function. I use nodejs to retrieve the values ​​of my arduino with my raspberry. I would like to know if you know how to use curl with nodejs because after several attempts I have failed. Thank

2 Answers2

1

If you referring to PHP curl, then all you need to know that node.js has no "curl" (which stands for "Client URL"). It has http module with request and get methods in order to make http requests. Which have same purpose as PHPs curl.

You still can execute shell script using child_process.exec, but doing so you risk that application that you will run through exec might be platform dependent, in that case it might work on windows and will not on linux or any similar issue. For xively you should use http.request - and that will do all the job for you. And it will be easier, as getting output from curl via child_process.exec will be much harder than through http.request.

So based on documentation in here: https://xively.com/dev/tutorials/curl/
I have made http.request alternative in order to query their feed:

var data = JSON.stringify({
  title: 'My feed',
  version: '1.0.0'
});

var req = http.request({
  host: 'api.xively.com',
  path: '/v2/feeds',
  method: 'POST',
  headers: {
    'X-ApiKey': 'YOUR_API_KEY_HERE',
    'Content-Type': 'application/json',
    'Content-Length': data.length
  }
}, function(res) {
  res.setEncoding('utf8');
  console.log('Status Code: ' + res.statusCode);
  console.log('Headers:');
  console.log(res.headers);

  var buffer = '';
  res.on('data', function(chunk) {
    buffer += chunk;
  });
  res.on('end', function() {
    console.log('complete');
    console.log(buffer);
  });
});

res.write(data);
res.end();

I've wrote this code in browser, and not sure is fully correct, please try it out and let me know if there is anything wrong, that I will correct code above.
As you can see it supports all functionalities as CURL offers, and is more flexible and nicer from usage point of view.

Feel free to use them in order to make HTTP calls.

moka
  • 22,846
  • 4
  • 51
  • 67
  • And with node.js how execute a shell script? – user2572562 Jul 11 '13 at 14:50
  • I've updated answer above. But please, use `http.request` it is same as curl, and easier in use. As with `child_process.exec` calling `curl` will be more complicated to use and more coding as well. Without any advantage though. – moka Jul 11 '13 at 14:58
  • But it's possible to execute a script shell? The curl is this how to in nodejs? curl --request PUT \ --data-binary '{"datastreams":[ { "id": "1'" , "tags": "'1", "current_value": "12" }]}' \ --header "X-ApiKey:Q" \ --verbose \ https://api.xively.com/v2/feeds/ – user2572562 Jul 11 '13 at 15:06
0

But it's possible to execute a script shell?

The curl is this how to in nodejs? curl --request PUT \ --data-binary '{"datastreams":[ { "id": "1'" , "tags": "'1", "current_value": "12" }]}' \ --header "X-ApiKey:Q" \ --verbose \ https://api.xively.com/v2/feeds/