Home > Software engineering >  What must I do to use Axios in node js to access an API endpoint?
What must I do to use Axios in node js to access an API endpoint?

Time:12-06

I have used Postman to explore an API for Samsung's SmartThings. I have that working as expected. But when I take that information and try to implement it in node with Axios the data returned looks like it is compressed or some other blob. Here is the code I am trying to use to get the response:

const axios = require("axios");

function main() {
  const st_api = axios.create();
  st_api
    .get("https://api.smartthings.com/v1/locations/", {
      headers: {
        get: {
          Accept: "application/vnd.smartthings json",
        },
        Authorization: process.env.my_home_token,
      },
      responseType: "json",
      responseEncoding: "utf8",
      decompress: true,
    })
    .then(function (res) {
      console.log("Status: ", res.status);
      console.log("Data: ", res.data);
    })
    .catch(function (err) {
      console.log("Error: ", err);
    });
}

main();

And the console log returned is:

Status:  200
Data:  �$ͱ�0��W1wn��-H�����d���6KK��»[�v�/���������BC���BXn���%�ek3��j��&�� m�x�M��i�i1 ��פ��8�`�����4�r_����d�ޤ�A�Z��K�K��UV�rk²�<\�_㿻���wA��

I have tried inserting gzip to decompress it or other header information but nothing changes. I am expecting some json returned (or at least something human readable would be a start). I believe I distilled my simple (I hope) api call to a brief test and nothing I try has changed the data being returned.

CodePudding user response:

This is a bug in Axios. Downgrade your Axios version to 1.1.3 or lower, and don't upgrade until 1.3.0 is released. Ref: https://github.com/axios/axios/pull/5300, https://github.com/axios/axios/pull/5306

  • Related