简体   繁体   English

gzip解压post请求,axios,Node.JS

[英]Gzip decompression post request, axios, Node.JS

I'm doing a post request using axios in node.js. The response is gzip data (behind, it's a huge json)我正在使用 node.js 中的 axios 进行发布请求。响应是 gzip 数据(后面是一个巨大的 json)

My goal is to read the json file behind the res (gzip).我的目标是读取 res (gzip) 后面的 json 文件。

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... ��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�������zhʫ�c1�A�����.�t8������,���� ...

Does someone have any suggestion?有人有什么建议吗? Thanks !谢谢 !

In my case, I want to get the information of my accessToken (from Google provider), then I can send a GET request like this:就我而言,我想获取我的 accessToken 信息(来自 Google 提供商),然后我可以发送这样的 GET 请求:

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.然后我收到看起来与您相似的data I investigate and find out that the data is compressed with gzip , then to use it we must decompress the data .我调查并发现data是用gzip压缩的,然后要使用它我们必须解压缩data

Now I use zlib.现在我使用 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"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM