Skip to content

Instantly share code, notes, and snippets.

@WhiteX
Forked from dtoki/PostData.js
Created April 7, 2022 18:14
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 WhiteX/9bb2ba1a4fee35ca46c08fc8e24cc7c7 to your computer and use it in GitHub Desktop.
Save WhiteX/9bb2ba1a4fee35ca46c08fc8e24cc7c7 to your computer and use it in GitHub Desktop.
Sending data to a web hook using javascript XMLHttpRequest #clientside
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