Skip to content

Instantly share code, notes, and snippets.

@dtoki
Last active November 15, 2022 16:44
Show Gist options
  • Save dtoki/1dae9eec72e0f1efb23ff3536b710391 to your computer and use it in GitHub Desktop.
Save dtoki/1dae9eec72e0f1efb23ff3536b710391 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);
}
@HJCDstudios
Copy link

Thanks ill try it out later in my website

@HJCDstudios
Copy link

Looks like this is not what i wanted thanks anyway

@PokeyManatee4
Copy link

This Is Going To Be Funny On Load For My Slack Webhook HAHAHAHAHA

@PokeyManatee4
Copy link

Thanks.
Screenshot 2021-10-06 10 40 15 PM

@WhiteX
Copy link

WhiteX commented Apr 7, 2022

Saving this for later. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment