Nodejs post trouble to cloud api [SOLVED]

Hey All,
I need some help using node.js to make a http post to call my function on the spark core. I am definitely missing something fundamental.

var http = require('http');

var data = JSON.stringify({
  "access_token": "123123213213123",
  "args": "990"
});

var options = {
  host: 'api.spark.io',
  port: 443,
  path: '/v1/devices/1231231232131231213/myFunc',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(data)
  }
};

var req = http.request(options, function(res) {
  var result = '';

  res.on('data', function(chunk) {
    result += chunk;
  });

  res.on('end', function() {
    console.log(result);
  });
});

req.on('error', function(err) {
  console.log(err);
});

req.write(data);
req.end();

I keep getting: { [Error: socket hang up] code: ‘ECONNRESET’ }

note: myFunc is registered/defined and takes an argument of string on my core.

Please help.

Thanks!

@SomeFixItDude, this code works for me, I am using it in a Node-RED node.

var request = require('request');
.
.
.
request.post(
	'https://api.spark.io/v1/devices/' + this.core_id + '/' + this.func_name,
	{ 
		form: { 
			access_token: this.access_token,
			args: '<arguments>' 
		} 
	},
	function (error, response, body) {
		if (!error && response.statusCode == 200) {
			console.log(body)
		}
	});
1 Like

@krvarma that did it!! thanks so much!