Search code examples
javascriptnode.jsapigzipslack-api

api response has a file, unable to extract contents of file


I am building a NodeJS app where i have to use a Slack API. So using a GET request i call the API and i get the response back in a gunzip file. Once i uncompress the gunzip, the contents inside are just data in json format.
I am able to call the Slack API and get back the response but i am unable to uncompress the file contents.
I found that we can use "file-saver" for this.
Here is the code:

const response = await fetch(URL, obj);

    if (!response.ok) {
      throw new Error(response);
    }

    // Extract filename from header
    const filename = response.headers.get('content-disposition')
      .split(';')
      .find(n => n.includes('filename='))
      .replace('filename=', '')
      .trim()
    ;

    const blob = new Blob([JSON.stringify(response.body)], { type: 'text/plain' });

    // Download the file
    saveAs(blob, filename);


This doesn't save any file. I just get a response back from the API.
API response:

 type: 'default',
    url: 'https://slack.com/api/admin.analytics.getFile?type=member&date=2022-09-20&pretty=1',
    status: 200,
    statusText: 'OK',
    headers: {
      'access-control-allow-headers': 'slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, x-b3-sampled, x-b3-flags',
      'access-control-allow-origin': '*',
      'access-control-expose-headers': 'x-slack-req-id, retry-after',
      'cache-control': 'private',
      connection: 'close',
      'content-disposition': `attachment; filename="Analytics_2022_09_20.json.gz"; filename*=utf-8''Analytics%202022-09-20.json.gz`,
      'content-type': 'application/gzip',
      date: 'Mon, 10 Oct 2022 04:20:55 GMT',
      expires: 'Mon, 26 Jul 1997 05:00:00 GMT',
      pragma: 'private',
      'referrer-policy': 'no-referrer',


I also found the we can use "zlib" to extract the contents of the gunzip file.
I couldn't get this to work, here is the code:

import {gzip, gunzip} from "zlib";

var options = {
                url: URL,
                headers: {
                    'api_key': 'Bearer ' +  process.env.accesstoken,
                    'Content-Type': 'application/json',
                    'Accept-Encoding': 'gzip'
                },
                encoding: null 

            };

        request(options, function(err, response, body){
        
                var encoding = response.headers['content-encoding']

                if (encoding && encoding.indexOf('gzip') >= 0) {

                  gunzip(body, function(err, dezipped) {
                    var json_string = dezipped.toString('utf-8');
                    var json = JSON.parse(json_string);
                    console.log('JSON content: ',json);
                  });

                } else {
                    console.log('not gzipped!');
                }

        })  



In this the response i get back from API itself is different.
How else can i extract the gunzip file contents? ANy suggestions would be great!!


Solution

  • This is the code that worked for me:

    request(URL, {     //fetch slack data from API
        headers: {
            Authorization: 'Bearer ' +  process.env.accesstoken,
        }})
            .pipe(zlib.createGunzip())
            .pipe(
            concat((stringBuffer) => {
                result = stringBuffer.toString()
                fs.writeFileSync('gzip-file.json', result, (error) => {
                    if (error) throw error;
                  });
            })
            );