使用松弛API上传时no_file_data错误

时间:2019-01-31 18:02:31

标签: coffeescript slack-api hubot

我得到这个错误{“OK”:假的,“错误”:“no_file_data”}当我尝试调用API松弛上传。我已经在./log中有指定的文件。

robot.respond /upload (.*)/i, (res) ->
    app_name = res.match[1]
    request = require("request")
    fs = require("fs")
    channel_name = res.message.room

    data = {
        channels: channel_name,
        filename: "#{app_name}.txt",
        file: fs.createReadStream("./log/#{app_name}.txt"),
    }

    robot.http("https://slack.com/api/files.upload")
      .headers(Authorization: 'Bearer slack-token', 'Content-Type': 'application/json')
      .post(data) (err, httpRes, body) ->
        if err
          res.send "#{err}"
          return

        res.send "#{body}"

1 个答案:

答案 0 :(得分:0)

这不起作用,因为API方法files.upload不支持内容类型application/json的帖子。

对于您的情况,您需要以multipart/form-data的形式发布,其中一部分包含文件数据,另一部分包含API参数(包括令牌)。

相关问题