-
-
Save WhiteX/9bb2ba1a4fee35ca46c08fc8e24cc7c7 to your computer and use it in GitHub Desktop.
Sending data to a web hook using javascript XMLHttpRequest #clientside
This file contains hidden or 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
function postDataToWebhook(data){ | |
//get the values needed from the passed in json object | |
var userName=data.name; | |
var userPlatform=data.platform; | |
var userEmail=data.email; | |
//url to your webhook | |
var webHookUrl="webhook_url"; | |
//https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest | |
var oReq = new XMLHttpRequest(); | |
var myJSONStr = payload={ | |
"text": "Acuired new user", | |
"attachments":[ | |
{ | |
"author_name": userName, | |
"author_icon": "http://icons.iconarchive.com/icons/noctuline/wall-e/128/Wall-E-icon.png", | |
"color": "#7CD197", | |
"fields":[ | |
{ | |
"title":"Platform", | |
"value":userPlatform, | |
"short":true | |
}, | |
{ | |
"title":"email", | |
"value":userEmail, | |
"short":true | |
} | |
] | |
} | |
] | |
}; | |
//register method called after data has been sent method is executed | |
oReq.addEventListener("load", reqListener); | |
oReq.open("POST", webHookUrl,true); | |
oReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
oReq.send(JSON.stringify(myJSONStr)); | |
} | |
//callback method after webhook is executed | |
function reqListener () { | |
console.log(this.responseText); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment