我正在使用node.js中的axios做一个post请求。响应是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�;����_��C�����۬���?q$@�CFq���ŗ��Ӹ6j%��M������Էʫ�c1��A�����.�t8�����Ș
有人有什么建议吗?谢谢!
发布于 2022-11-23 05:30:05
在我的例子中,我想获得我的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"
}
https://stackoverflow.com/questions/70377802
复制相似问题