Skip to content

Instantly share code, notes, and snippets.

@cpq
Last active July 5, 2019 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpq/d875954390a6fecfc899beaed027fe83 to your computer and use it in GitHub Desktop.
Save cpq/d875954390a6fecfc899beaed027fe83 to your computer and use it in GitHub Desktop.
var https = require('https');
// Same as:
// curl -H 'Content-Type: application-json' -d PARAMS
// https://mdash.net/api/v2/devices/DEVICE_ID/rpc/METHOD?access_token=TOKEN
var mdashRequest = function(apiToken, deviceID, method, params, func) {
return new Promise(function(resolve, reject) {
var received = '';
var strParams = JSON.stringify(params || {});
var url = 'https://mdash.net/api/v2/devices/' + deviceID + '/rpc/' +
method + '?access_token=' + apiToken;
var options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': strParams.length
}
};
console.log('URL', url);
var req = https.request(url, options, function(res) {
res.on('data', data => received += data);
res.on('end', () => resolve(received));
});
req.on('error', err => reject(err));
req.write(strParams);
req.end();
});
};
mdashRequest('xxxxxxxxxx', 'd6', 'setPin', {pin: 2, val: 1})
.then(response => console.log('RESPONSE:', response))
.catch(err => console.error('ERROR:', err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment