Last active
November 15, 2022 16:44
-
-
Save dtoki/1dae9eec72e0f1efb23ff3536b710391 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); | |
} |
lifesaver
If you want to send JSON you should set: oReq.setRequestHeader('Content-Type', 'application/json');
I am trying to make it work with discord webhook, does anyone know how to do it?
Thanks in advance!
Thanks ill try it out later in my website
Looks like this is not what i wanted thanks anyway
This Is Going To Be Funny On Load For My Slack Webhook HAHAHAHAHA
Saving this for later. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how do i get this to work with discord?