【问题标题】:Cannot make POST request to webhook无法向 webhook 发出 POST 请求
【发布时间】:2020-02-12 17:30:51
【问题描述】:

我试图向 discord api 发出 POST 请求,但它返回 {"message": "Cannot send an empty message", "code": 50006} 我也尝试过使用 FormData 来发布内容,以及一个普通的字符串。

function loadDoc()
{
    let json = JSON.stringify({
        "content": "test",
      });

    const http = new XMLHttpRequest()
    http.open("POST", "https://discordapp.com/api/webhooks/MYWEBHOOK",true)
    http.setRequestHeader("Content-Type","multipart/form-data")
    http.send(json)
    http.onload = () => console.log(http.responseText)
}

【问题讨论】:

    标签: javascript xmlhttprequest discord


    【解决方案1】:

    也许采取一种方法来获取:

    let url = 'YOUR_URL';
    let content = {YOUR_DATA};
    
    let optionalParam = {
        headers: {
        "content-type":"application/json; charset=UTF-8"
      },
      body: content,
      method: "POST"
    };
    
    fetch(URL, optionalParam)
    .then(data => {return data.json()})
    .then(res => {console.log(res)})
    .catch(error => {console.log(error)})
    

    【讨论】:

    • 非常感谢,尽管您的示例确实包含语法错误。这对我有用function loadDoc() { const url = 'https://discordapp.com/api/webhooks/677059599391522827/MYWEBHOOK'; const user = { content: 'test' }; const options = { method: 'POST', body: JSON.stringify(user), headers: { 'Content-Type': 'application/json' } } fetch(url, options) .then(res => res.json()) .then(res => console.log(res)) }
    • 酷。很高兴我能提供帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 2016-01-04
    • 1970-01-01
    • 2019-08-27
    相关资源
    最近更新 更多