简体   繁体   English

我必须做什么才能在 node.js 中使用 Axios 来访问 API 端点?

[英]What must I do to use Axios in node js to access an API endpoint?

I have used Postman to explore an API for Samsung's SmartThings.我已经使用 Postman 探索了三星 SmartThings 的 API。 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.但是当我获取该信息并尝试在具有 Axios 的节点中实现它时,返回的数据看起来像是压缩的或其他一些 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.我尝试插入 gzip 来解压缩它或其他 header 信息,但没有任何变化。 I am expecting some json returned (or at least something human readable would be a start).我期待一些 json 返回(或者至少是人类可读的东西)。 I believe I distilled my simple (I hope) api call to a brief test and nothing I try has changed the data being returned.我相信我将我的简单(我希望)api 调用提炼成一个简短的测试,我尝试的任何事情都没有改变返回的数据。

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.这是 Axios 中的一个错误。将您的 Axios 版本降级到 1.1.3 或更低版本,并且在 1.3.0 发布之前不要升级。 Ref: https://github.com/axios/axios/pull/5300 , https://github.com/axios/axios/pull/5306参考: https://github.com/axios/axios/pull/5300,https ://github.com/axios/axios/pull/5306

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM