如何使用 node-fetch 通过 discord webhook 发送图像?

how to send image via discord webhook using node-fetch?

const fetch = require('node-fetch');
const fs = require('fs')
var data = fs.readFileSync('2.png', 'utf8')
var URL = "apikey";
fetch(URL, {
     "method":"POST",
     "headers": {"Content-Type": "application/json"},
     "body": data
    })
    .then(res=> console.log(res))
    .catch(err => console.error(err));

如何通过 discord webhook 发送图像?我已经尝试了上述方法,但它不起作用。并且在 discord 文档中没有合适的示例。

您正在发送文件,那么您的 Content-Type 应该是 multipart/form-data

将代码更改为

const fetch = require('node-fetch');
const formData = require('form-data');
const fs = require('fs')

const form = new formData();
form.append('file1', fs.createReadStream('./2.png')); // give absolute path if possible

var URL = "XYZ URL";

fetch(URL, {
    'method': 'POST',
    'body': form,
    headers: form.getHeaders()
})
.then(res=> console.log(res))
.catch(err => console.error(err));

discord's documentations

本页黄色提示