Search code examples
javascriptnode.jspostaxiosslack

Slack API upload string as file


I have a sting variable of a csv. I want to upload it to a slack channel as a .csv file, not as text.

async function run() {
    const csvData = 'foo,bar,baz';    
    const url = 'https://slack.com/api/files.upload';
    const res = await axios.post(url, {
        channel: '#csvchannel',
        filename: 'CSVTest.csv',
        content: csvData
    }, { headers: { authorization: `Bearer ${slackToken}` } });

    console.log('Done', res.data);
}

This code returns: error: 'no_file_data', Changing content to file gives the same response.

What do I have to do to convert the csv sting into a file that can be uploaded? I can't use fs to write out the file.

I have tried to use fs.createReadStream(csvData) but that needs a file, not a string.

Slack API documentation: https://api.slack.com/methods/files.upload


Solution

  • You don't need to convert the CSV into a file, seems you are missing a couple of things here:

    • fileType property, it needs to be CSV.
    • Slack file upload API supports multipart/form-data and application/x-www-form-urlencoded content types. You're missing the Content-Type.

    Check out a working example of how you could send the data using application/x-www-form-urlencoded

    Send a CSV to Slack                                                                                 View in Fusebit
    const csvData = 'foo,bar,baz';
    const url = 'https://slack.com/api/files.upload';
    const params = new URLSearchParams()
    params.append('channels', slackUserId);
    params.append('content', csvData);
    params.append('title', 'CSVTest');
    params.append('filetype', 'csv');
    
    const result = await axios.post(url, params,
      {
        headers:
      {
        authorization: `Bearer ${access_token}`,
        'Content-Type': 'application/x-www-form-urlencoded'
        }
     });
    
    ctx.body = { message: `Successfully sent a CSV file to Slack user ${slackUserId}!` };