Created
November 30, 2016 07:42
-
-
Save otymartin/10351fa527697b316bec815d5025890d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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