我正在尝试使用 slack api https://api.slack.com/methods/files.upload 将图像上传到 slack .
function uploadImage(file) {
var form = new FormData();
form.append('image', file);
var self = this;
const config = {
headers: {
'content-type': 'multipart/form-data'
}
}
var token = 'myToken'
axios.post('https://slack.com/api/files.upload?token=' + token, form, config)
.then(function (response) {
console.log(response);
self.imageUploaded("Image uploaded")
})
.catch(function (error) {
console.log(error);
self.error(error);
})
}
我收到“无效表单数据”的响应。知道可能出了什么问题吗?
谢谢和最好的问候!
PS:我可以用python发布一张图片来松弛
def post_image(filename, token, channels):
f = {'file': (filename, open(filename, 'rb'))}
response = requests.post(url='https://slack.com/api/files.upload',
data={'token': token, 'channels': channels},
headers={'Accept': 'application/json'},
files=f)
return response.tex
只需要使用公理提出相同的请求
最佳答案
您可以使用带有表单数据库的 axios 上传文件
const fs = require("fs");
const axios = require("axios");
const FormData = require("form-data");
const form = new FormData();
form.append("token", 'token here');
form.append("channels", channelId);
form.append("file", fs.createReadStream(__dirname + "/aapl.png"), 'file name');
try {
const res = await axios.post("https://slack.com/api/files.upload", form, {
headers: form.getHeaders(),
});
} catch (err) {
throw new Error(err);
}
关于javascript - 使用 axios 上传图片到 Slack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43607011/