You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Discord Webhook时持续返回403 Forbidden错误求助

Discord Webhook 403 Forbidden 问题解决方案及附件发送实现

403错误的核心原因

你当前用前端直接通过CORS Anywhere转发请求到Discord Webhook,存在两个致命问题:

  • Discord Webhook接口不支持前端跨域请求,即使通过代理中转,Discord的反拦截机制会识别这类代理请求并返回403。
  • Webhook URL直接暴露在前端代码中,极易被恶意抓取滥用,Discord也会因异常请求频率封禁该Webhook。

正确解决方案:后端中转请求

前端不要直接调用Discord Webhook,而是先把数据发送到自己的后端服务,由后端转发请求到Discord。这样既解决跨域问题,又能保护Webhook URL不泄露。

示例后端代码(Node.js/Express)

const express = require('express');
const axios = require('axios');
const app = express();
app.use(express.json());

// 前端请求该接口中转消息
app.post('/send-to-discord', async (req, res) => {
  const { fileName, fileContent } = req.body;
  const discordWebhookUrl = '你的Discord Webhook URL'; // 仅在后端存储,不暴露给前端

  try {
    const payload = {
      content: "New file detected:",
      embeds: [
        {
          title: fileName,
          description: "File content:",
          fields: [
            {
              name: "Content",
              value: fileContent.slice(0, 500) + "...",
            },
          ],
        },
      ],
    };
    await axios.post(discordWebhookUrl, payload, {
      headers: { 'Content-Type': 'application/json' }
    });
    res.status(200).send('消息发送成功');
  } catch (error) {
    console.error('发送失败:', error);
    res.status(500).send('发送失败');
  }
});

app.listen(3000, () => console.log('后端服务运行在3000端口'));

前端修改后的请求代码

// 替换原代码中的fetch逻辑
fetch('http://localhost:3000/send-to-discord', {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    fileName: file.name,
    fileContent: content
  }),
})
.then((response) => {
  if (response.ok) {
    console.log("Message sent to Discord!");
  } else {
    console.error("发送失败,状态码:", response.status);
  }
})
.catch((error) => {
  console.error("Error sending message to Discord:", error);
});

实现文件附件发送

若要直接发送文件作为附件,后端需构造multipart/form-data格式的请求:

后端发送附件示例(Node.js)

const FormData = require('form-data');
const fs = require('fs');
const axios = require('axios');

// 处理前端上传的文件并转发到Discord
app.post('/send-file-to-discord', async (req, res) => {
  const discordWebhookUrl = '你的Discord Webhook URL';
  const formData = new FormData();
  
  // 添加消息文本
  formData.append('payload_json', JSON.stringify({ content: '收到新附件' }));
  // 添加文件(示例为本地文件,实际可从前端上传的文件流获取)
  formData.append('file', fs.createReadStream('./uploaded-file.txt'));

  try {
    await axios.post(discordWebhookUrl, formData, {
      headers: formData.getHeaders()
    });
    res.status(200).send('附件发送成功');
  } catch (error) {
    console.error('附件发送失败:', error);
    res.status(500).send('附件发送失败');
  }
});

临时测试方案(不推荐生产环境使用)

如果只是临时测试不想搭建后端,可以先访问你使用的CORS Anywhere实例主页,点击"Request temporary access to the demo server"获取临时授权,之后再运行前端代码,可能绕过403拦截,但Webhook URL暴露的风险依然存在。

内容的提问来源于stack exchange,提问作者abcd efgh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 22:24:53