简体   繁体   English

使用JWT进行axios REST调用

[英]axios REST call with JWT

I am running a local REST-API. 我正在运行本地REST-API。 When I test it with Postman I get the following results: 当我用Postman测试它时,得到以下结果:

  1. get JWT with same params and URL shown in the code below 获得具有相同参数和URL的JWT,如下面的代码所示
  2. use that obtained JWT in a request to get data with the same params and 'X-authorization' header as shown in the code below. 在请求中使用获得的JWT来获取具有相同参数和“ X-authorization”标头的数据,如下面的代码所示。

When I do the same with axios I get a permission error: 当我对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);
          });
      });

The problem: The second request fails and returns a permission error. 问题:第二个请求失败,并返回权限错误。

Screenshot1: Error in console 屏幕截图1:控制台错误 在此处输入图片说明

Screenshot2: Succes in Postman 屏幕截图2:邮递员成功 在此处输入图片说明

Screenshot3: Request-headers 屏幕截图3:请求标头 在此处输入图片说明

The problem was a setting on the rest server. 问题是其余服务器上的设置。 There was a minimum delay between getting JWT and data request. 在获取JWT和数据请求之间有最小的延迟。

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

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