2

I'm doing a post request using axios in node.js. The response is gzip data (behind, it's a huge json)

My goal is to read the json file behind the res (gzip).

Currently, my request is:

await axios({
  method: "post",
  url: process.env.API_URL + "/collection",
  headers: {
    "Content-Type": "application/json",
    "Accept-Encoding": "gzip, deflate, br",
  },
  data: {
    project: req.body.project,
    platform: req.body.platform,
  },
  decompress: true,
}).then(async (response) => {
  console.log(response.data);
});

But I receive data like:

�1�����Q��:GR}��"-��}$K�ևҹ\��°<ܖqw�Vmp�������Y!�����܋a�F�]� ���K%}0�rЈ^�<��/�> ��Q���C7��R>�]§.,j�rg�6�MUVH��_Xq�����}|��a����$����K��cˠ��[�vv�����o�6�v�?~�����h���'Kn.��e��ZUW�;���ŗ��Ӹ׿6j%��M������Էʫ�c1��A�����.�t8�����Ș,����_��C�����۬���?q$޽@�CFq...

Does someone have any suggestion? Thanks !

MarioG8
  • 5,122
  • 4
  • 13
  • 29
JAKK
  • 53
  • 2
  • 7
  • Have you tried placing `response.data` into the browser (e.g. a `span` element) instead of through `console`. Modern browsers automatically de-gzip. – JMP Dec 16 '21 at 11:13
  • Doesn't look like there is a browser involved. Maybe the decompressing in axios doesn't work properly. Can you try [this answer with zlib](https://stackoverflow.com/a/63858855/1948292). – Daniel W. Dec 16 '21 at 11:17
  • It logs me: when I use zlib.gunzip(response, function (_err, output) { console.log(output.toString()); }); – JAKK Dec 16 '21 at 11:22
  • @JMP, the fact is that I can't juste do that because I'm doing some processing after decompressing the answer... – JAKK Dec 16 '21 at 11:24
  • Maybe you accidently compressed the file 2 times. Make sure there is no double-compressing happening. – Daniel W. Dec 16 '21 at 12:11
  • 1
    the problem is that axios does not support brotli by default. Here's their reported issue: https://github.com/axios/axios/issues/1635 – marcellsimon Apr 02 '22 at 19:58

1 Answers1

0

In my case, I want to get the information of my accessToken (from Google provider), then I can send a GET request like this:

const googleOauth2Url = `https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=${accessToken}`;

const { data } = await axios.get(googleOauth2Url, {
        responseType: "arraybuffer",
        decompress: true,
      });

Then I receive the data that looks similar to yours. I investigate and find out that the data is compressed with gzip, then to use it we must decompress the data.

Now I use zlib.

zlib.gunzip(data, function (error, result) {
        console.log(result.toString());
        return result.toString();
      });

And the final result is:

{
  "issued_to": "some data,
  "audience": "some data",
  "user_id": "some id",
  "scope": "openid https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile",
  "expires_in": 2971,
  "email": "sample@gmail.com",
  "verified_email": true,
  "access_type": "offline"
}
peterburgs
  • 36
  • 3