簡體   English   中英

gzip解壓post請求,axios,Node.JS

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

我正在使用 node.js 中的 axios 進行發布請求。響應是 gzip 數據(后面是一個巨大的 json)

我的目標是讀取 res (gzip) 后面的 json 文件。

目前,我的要求是:

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);
});

但我收到如下數據:

��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������,���� ...

有人有什么建議嗎? 謝謝 !

就我而言,我想獲取我的 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,
      });

然后我收到看起來與您相似的data 我調查並發現data是用gzip壓縮的,然后要使用它我們必須解壓縮data

現在我使用 zlib。

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

最后的結果是:

{
  "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