【问题标题】:How do I send an image via discord webhook using node-fetch?如何使用 node-fetch 通过 discord webhook 发送图像?
【发布时间】:2021-10-21 03:22:31
【问题描述】:
const fetch = require('node-fetch');
const fs = require('fs')
var data = fs.createReadStream('2.png')
const fileSizeInBytes = data.size;
var URL = "apikey";
fetch(URL, {
     "method":"POST",
     "headers": {
         "Content-length": fileSizeInBytes,
         'Content-Type': 'multipart/form-data',
         'Content-Disposition': 'form-data; name="2"; filename="2.png"'
         },
     "body": JSON.stringify({
        "file":data
      })
    })
    .then(res=> console.log(res))
    .catch(err => console.error(err));

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

【问题讨论】:

    标签: javascript node.js discord fetch webhooks


    【解决方案1】:
    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));
    

    【讨论】:

      猜你喜欢
      • 2021-10-18
      • 2020-11-28
      • 2020-04-11
      • 2020-11-26
      • 2019-06-07
      • 1970-01-01
      • 2019-01-15
      • 2023-02-21
      • 2019-10-21
      相关资源
      最近更新 更多