discord - Is there any way to post a webhook using JavaScript? - Stack Overflow

I want to send a message in my Discord server using a Webhook. Is there any way to do that using JavaSc

I want to send a message in my Discord server using a Webhook. Is there any way to do that using JavaScript?

I tried this code, but it didn't work:

var url = // Webhook URL
var text = '{"content":"Hi"}' // Text to post
$.ajax({
   data: 'payload=' + JSON.stringify({
    "text": text
   }),
   dataType: 'json',
   processData: false,
   type: 'POST',
   url: url
});

Souce.

I want to send a message in my Discord server using a Webhook. Is there any way to do that using JavaScript?

I tried this code, but it didn't work:

var url = // Webhook URL
var text = '{"content":"Hi"}' // Text to post
$.ajax({
   data: 'payload=' + JSON.stringify({
    "text": text
   }),
   dataType: 'json',
   processData: false,
   type: 'POST',
   url: url
});

Souce.

Share Improve this question edited Oct 31, 2023 at 21:04 NebularNerd 1022 silver badges10 bronze badges asked May 5, 2018 at 8:09 Adit LuhadiaAdit Luhadia 4121 gold badge8 silver badges18 bronze badges 2
  • @Boy With Silver Wings Thanks a lot! Hope it works :) – Adit Luhadia Commented May 5, 2018 at 8:29
  • 2 if you want to post a json you should specify ` contentType: "application/json"` in your request. Also 'payload={"text": "..."}' is not a valid json payload. – nutic Commented May 5, 2018 at 8:34
Add a ment  | 

2 Answers 2

Reset to default 3
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Discord Webhook Tutorial</title>
  </head>
  <body>
    <button onclick="sendMessage()">Send</button>
  </body>

  <script>
    function sendMessage() {
      var request = new XMLHttpRequest();
      request.open("POST", "https://discordapp./api/webhooks/676118118082281513/ZS5YcWhurzokBrKX9NgexqtxrJA5Pu2Bo4i7_JsIxC-JIbPBVhSZkcVVukGOro52rnQA");

      request.setRequestHeader('Content-type', 'application/json');

      var params = {
        username: "My Webhook Name",
        avatar_url: "",
        content: "The message to send"
      }

      request.send(JSON.stringify(params));
    }
  </script>
</html>

A WebHook url is still a Universal Resource Locator is it not? They are typically defined on Http so if you want to call a Webhook you would do so over Http. This means a typical fetch request can make a WebHook call.

In your case, you are posting a “Hi” message to the WebHook so it should look something like this:

var url = // Webhook URL
var text = {content: "Hi"}

fetch(url, 
  {
    method: “POST”,
    headers: {“Content-Type”: “application/json”},
    body: JSON.stringify(text),
  }
)
  .then(res => •••); /*Do whatever you want with the response when it arrives.*/

The assumption is that the WebHook expects a payload with Content-Type”: “application/json” via a Post request at that endpoint.

AFAIK, WebHooks are a way servers municate with each other not client-server munication but that’s disputable.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744266562a4565892.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信