使用JWT进行axios REST调用

问题描述 投票:0回答:1

我正在运行本地REST-API。当我用Postman测试它时,我得到以下结果:

  1. 使用下面的代码中显示的相同参数和URL获取JWT
  2. 使用获得JWT的请求来获取具有相同参数和“X-authorization”标题的数据,如下面的代码所示。

当我对axios做同样的事情时,我得到一个权限错误:

    axios
      .request({
        method: "get",
        baseURL: "http://localhost/api/",
        params: {
          action: "login",
          username: "user",
          password: "pass"
        }
      })
      .then(function(res) {
        const token = res.data.JWT; //token is correct
        axios
          .get(
            "http://localhost/api/index.php?action=list&object=media",
            {
              headers: {
                "X-Authorization": "Bearer " + token
              }
            }
          )
          .then(function(res) {
            console.log(res);
          });
      });

问题:第二个请求失败并返回权限错误。

截图1:控制台enter image description here出错

截图2:Postman enter image description here中的成功

截图3:请求标头enter image description here

javascript rest axios
1个回答
0
投票

问题是休息服务器上的设置。获取JWT和数据请求之间存在最小延迟。

© www.soinside.com 2019 - 2024. All rights reserved.