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 badges
Add a ment  | 

1 Answer 1

Reset to default 8

In 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