Skip to content

Instantly share code, notes, and snippets.

@otymartin
Created November 30, 2016 07:42
Show Gist options
  • Save otymartin/10351fa527697b316bec815d5025890d to your computer and use it in GitHub Desktop.
Save otymartin/10351fa527697b316bec815d5025890d to your computer and use it in GitHub Desktop.
var http = require('http');
var url = require('url');
function webhook(urlStr, data, cb) {
var postData = JSON.stringify(data);
var options = url.parse(urlStr);
options.method = 'POST';
options.headers = {'Content-Type': 'application/json', 'Content-Length': postData.length};
var req = http.request(options, function(res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
cb(null, body);
})
});
req.on('error', function(err) {
cb(err);
});
req.write(postData);
req.end();
}
// Function Webhook
Parse.Cloud.define('hello', function(request, response) {
var urlStr = ''; // TODO: replace with webhook URL
var data = {functionName: 'hello', params: request.params};
webhook(urlStr, data, function(err, data) {
if (err) {
response.error(err);
} else {
response.success(data);
}
});
});
// Trigger Webhook
Parse.Cloud.afterSave('Person', function(request) {
var urlStr = ''; // TODO: replace with webhook URL
var data = {triggerName: 'afterSave'};
webhook(urlStr, data, function(err, data) {
if (err) {
console.error(err);
} else {
console.log(data);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment