Skip to content Skip to sidebar Skip to footer

How To Upload An Image To Slack Using Node.js On Windows?

I'm trying to upload an image via Slack using node.js and the request package, but not having much luck. Either I receive invalid_array_arg or no_file_data errors from the API. Her

Solution 1:

The Slack API error invalid_array_arg means that there is a problem with the format of the arguments passed to Slack. (see here)

When using the file property for files.upload, Slack excepts the data as multipart/form-data, not as application/x-www-form-urlencoded. So instead of form, you need to use formData in your request object. I also removed the incorrect part in the header.

This works:

var fs = require('fs');
      var request = require('request');

      varSLACK_TOKEN = "xoxp-xxx";
      varSLACK_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) thrownewError(error);

      console.log(body);
    });

Solution 2:

In this sample script, it supposes to upload a binary file (zip file). When you use this, please modify for your environment. When files are uploaded to Slack, multipart/form-data is used. In my environment, there were the situations that files couldn't be uploaded by some libraries. So I created this. If this is useful for your environment, I'm glad.

Users can upload the binary file by converting byte array as follows.

  • At first, it builds form-data.
  • Adds the zip file converted to byte array and boundary using Buffer.concat().
  • This is used as body in request.

The sample script is as follows.

Sample script :

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);
    });
});

Post a Comment for "How To Upload An Image To Slack Using Node.js On Windows?"