在 v1.2.1 中修正了这个错误。
你需要添加Accept-Encoding
with application/json
在 axios.get 标头中。
它的默认值是gzip
在V1.2.0中
axios 在 v1.2.1 中修复了这个缺陷
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 在 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"
}
]
}