'Extract data from axios body array buffer response

I am calling an API that returns a gzip file in it's body. The problem I am having is I don't know how to extract specific data from that csv file as I need to use it further in my code. This is my code:

const response = await axios.request({
        method: 'GET',
        url: api,
        decompress: true,
        responseType: 'arraybuffer',
        headers: {
          Authorization: `Bearer ${token}`
        }
      }).then(resp => {
        zlib.gunzip(resp.data,(err,output) => {
          console.log(output);
        })
      });

The console log in the zlib callback displays the file as I want it but i don't know how to assign that value to response once the gunzip is finished.



Solution 1:[1]

Do you want the variable response to be set to output? If so you can just return output using an intermediary variable. I just called it out, but of course you can rename it to whatever seems fitting. Of course, you also need to handle the error cases, which I did not do below, but that should be a relatively easy task.

const response = await axios.request({
        method: 'GET',
        url: api,
        decompress: true,
        responseType: 'arraybuffer',
        headers: {
          Authorization: `Bearer ${token}`
        }
      }).then(resp => {
        let out;
        zlib.gunzip(resp.data,(err,output) => {
          out = output;
        })
        return out;
      });

Above the const response is set to the variable output in the gunzip func.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Big G