admin管理员组文章数量:1398761
I'm trying to invoke a REST call with Axios and getting a strange response.
try {
const response = await axios.get("/");
console.log(response.data);
}
catch (error) {
console.log(`[Error] -> ${JSON.stringify(error.response.data)}`)
}
It results in the following output:
▼♥��M
�0
5�Z�z�wg�4ӎ�W�����Yjxtʸ��:�Ǐ►_�☺}Y��/�R2~♠$���Ú�V�8\�]!�)☺*����H��>�§��Aj"↕��a0 ݚ��$�颲Y3%je3@=�0��@§lb:�|�}
.☺8�K&�_ƫp�j�(o-�ܚ♦
What I have to do to get a json object?
I tried to add headers headers with encoding and content type but without success.
I'm trying to invoke a REST call with Axios and getting a strange response.
try {
const response = await axios.get("https://api.predic8.de/shop/products/");
console.log(response.data);
}
catch (error) {
console.log(`[Error] -> ${JSON.stringify(error.response.data)}`)
}
It results in the following output:
▼♥��M
�0
5�Z�z�wg�4ӎ�W�����Yjxtʸ��:�Ǐ►_�☺}Y��/�R2~♠$���Ú�V�8\�]!�)☺*����H��>�§��Aj"↕��a0 ݚ��$�颲Y3%je3@=�0��@§lb:�|�}
.☺8�K&�_ƫp�j�(o-�ܚ♦
What I have to do to get a json object?
I tried to add headers headers with encoding and content type but without success.
Share Improve this question asked Nov 24, 2022 at 19:16 ClassfieldClassfield 632 silver badges7 bronze badges1 Answer
Reset to default 8In v1.2.1 fixed this error.
You need to add Accept-Encoding
with application/json
in axios.get header.
The default of it is gzip
in V1.2.0
axios fixed this defect in v1.2.1
demo code in v 1.2.0
const axios = require('axios')
const getProducts = async () => {
try {
const resp = await axios.get(
'https://api.predic8.de/shop/products/',
{
headers: {
'Accept-Encoding': 'application/json',
}
}
);
console.log(JSON.stringify(resp.data, null, 4));
} catch (err) {
// Handle Error Here
console.error(err);
}
};
getProducts();
OR fixed in v1.2.1
const axios = require('axios')
const getProducts = async () => {
try {
const resp = await axios.get(
'https://api.predic8.de/shop/products/'
);
console.log(JSON.stringify(resp.data, null, 4));
} catch (err) {
// Handle Error Here
console.error(err);
}
};
getProducts();
Result
$ node product.js
{
"meta": {
"count": 32,
"limit": 10,
"page": 1,
"next_url": "/shop/products/?page=2&limit=10"
},
"products": [
{
"name": "Bananas",
"product_url": "/shop/products/3"
},
{
"name": "Oranges",
"product_url": "/shop/products/10"
},
{
"name": "Pineapples",
"product_url": "/shop/products/33"
},
{
"name": "Dried Pineapples",
"product_url": "/shop/products/42"
},
{
"name": "Cranberries",
"product_url": "/shop/products/57"
},
{
"name": "Mango fresh",
"product_url": "/shop/products/62"
},
{
"name": "Raspberries",
"product_url": "/shop/products/90"
},
{
"name": "Cherries",
"product_url": "/shop/products/7"
},
{
"name": "Apple",
"product_url": "/shop/products/18"
},
{
"name": "Green Grapes",
"product_url": "/shop/products/11"
}
]
}
本文标签: javascriptNodeJs Axios response wrong encodingStack Overflow
版权声明:本文标题:javascript - NodeJs Axios response wrong encoding - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744684648a2619626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论