[英]How to upload an image to Slack using node.js on Windows?
我正在嘗試使用node.js和request包通過Slack上傳圖像,但是運氣不佳。 我從API收到invalid_array_arg
或no_file_data
錯誤。
這是我的要求:
var options = { method: 'POST',
url: 'https://slack.com/api/files.upload',
headers:
{ 'cache-control': 'no-cache',
'content-type': 'application/x-www-form-urlencoded' },
form:
{ token: SLACK_TOKEN,
channels: SLACK_CHANNEL,
file: fs.createReadStream(filepath)
} };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
我看了一些相關的文章:
唯一有效的方法是直接使用curl命令,但使用cygwin(CommandPrompt失敗: curl: (1) Protocol https not supported or disabled in libcurl
)。 從節點調用curl的問題(使用child_process
),但在命令提示符中無提示地失敗,並且仍然使用cygwin返回no_file_data
(將絕對路徑傳遞給文件):
stdout: {"ok":false,"error":"no_file_data"}
stderr: % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 469 100 35 100 434 359 4461 --:--:-- --:--:-- --:--:-- 6112
我在Windows上使用節點v6.9.1。
我想念什么? 如何在Windows上通過node.js將圖像上傳到Slack?
Slack API錯誤invalid_array_arg
表示傳遞給Slack的參數格式存在問題。 (請參閱此處 )
將file
屬性用於files.upload
,Slack files.upload
數據作為multipart/form-data
而不是application/x-www-form-urlencoded
。 因此,您需要在請求對象中使用formData
而不是form
。 我還刪除了標題中的不正確部分。
這有效:
var fs = require('fs');
var request = require('request');
var SLACK_TOKEN = "xoxp-xxx";
var SLACK_CHANNEL = "general";
var filepath = "file.txt";
var options = { method: 'POST',
url: 'https://slack.com/api/files.upload',
headers:
{ 'cache-control': 'no-cache' },
formData:
{ token: SLACK_TOKEN,
channels: SLACK_CHANNEL,
file: fs.createReadStream(filepath)
} };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
在此示例腳本中,它假設要上載一個二進制文件(zip文件)。 使用此功能時,請針對您的環境進行修改。 將文件上傳到Slack時,將使用multipart / form-data。 在我的環境中,有些情況是某些庫無法上傳文件。 所以我創建了這個。 如果這對您的環境有用,我很高興。
用戶可以通過如下轉換字節數組來上傳二進制文件。
示例腳本如下。
var fs = require('fs');
var request = require('request');
var upfile = 'sample.zip';
fs.readFile(upfile, function(err, content){
if(err){
console.error(err);
}
var metadata = {
token: "### access token ###",
channels: "sample",
filename: "samplefilename",
title: "sampletitle",
};
var url = "https://slack.com/api/files.upload";
var boundary = "xxxxxxxxxx";
var data = "";
for(var i in metadata) {
if ({}.hasOwnProperty.call(metadata, i)) {
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"" + i + "\"; \r\n\r\n" + metadata[i] + "\r\n";
}
};
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"file\"; filename=\"" + upfile + "\"\r\n";
data += "Content-Type:application/octet-stream\r\n\r\n";
var payload = Buffer.concat([
Buffer.from(data, "utf8"),
new Buffer(content, 'binary'),
Buffer.from("\r\n--" + boundary + "\r\n", "utf8"),
]);
var options = {
method: 'post',
url: url,
headers: {"Content-Type": "multipart/form-data; boundary=" + boundary},
body: payload,
};
request(options, function(error, response, body) {
console.log(body);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.