jonask
jonask

Reputation: 748

Unpack a gzip file that is content-disposition: attachment

I got the url to an api, when I visit the url in browser and pass my login, it will correctly download a zip file, that contains the JSON file with the data I want.

How do I get this JSON by fetching it with Node?

When I call like this

const data = await fetch(url, {
    headers: {
      Authorization: "Basic " + btoa(username + ":" + password)
    }
});

I correctly receive a data.body and data.headers but nothing near by the JSON data I am looking for.

Is it even possible to make an API call to a file that is attached?

Upvotes: 0

Views: 240

Answers (1)

jonask
jonask

Reputation: 748

My solution:

import request from "request";
import zlib from "zlib";
import concat from "concat-stream";

const btoa = (str) => Buffer.from(str).toString("base64");

request(url, {
  headers: {
    Authorization: "Basic " + btoa(username + ":" + password),
  }})
    .pipe(zlib.createGunzip())
    .pipe(
       concat((stringBuffer) => {
         console.log(stringBuffer.toString()) // gives me the json I was looking for
       })
     );

Upvotes: 1

Related Questions