最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

检查javascript头中是否存在api令牌

IT培训 admin 15浏览 0评论

检查javascript头中是否存在api令牌

我正在尝试测试我的API调用中是否存在API令牌。传递给的标题是-

"host": [
          {
            "key": "Host",
            "value": "mydomain"
          }
        ],
        "x-api-key": [
          {
            "key": "x-api-key",
            "value": "mykey"
          }
        ]
      }

现在我的代码看起来像

 // Check if apikey is a part of the headers 
    if ( !headers.hasOwnProperty('x-api-key') || headers['x-api-key'][0].value!="mykey") { 
        const body = 'Unauthorized';
        const response = {
            status: 401,
            statusDescription: 'Unauthorized',
            body: body,
        };
        callback(null, response);
    }

我的If案例出错,而不是如果标题完全丢失x-api-key,则不发送401

  "errorMessage": "Cannot read property '0' of undefined",

我应该如何更改条件,以便我可以同时检查键/值对,并且在缺少标题键/值的情况下也没有未定义的错误

回答如下:

尝试将您的代码更新为

 // Check if apikey is a part of the headers 
    if ( !headers.hasOwnProperty('x-api-key') || (headers.hasOwnProperty('x-api-key') && headers['x-api-key'][0].value!="mykey")) { 
        const body = 'Unauthorized';
        const response = {
            status: 401,
            statusDescription: 'Unauthorized',
            body: body,
        };
        callback(null, response);
    }

您的第一个条件将检查标题中是否存在x-api-key。第二个条件检查x-api-key的值。

OR的工作方式是,它将在第一次出现true条件时停止执行,或者将处理每个条件。因此,在您的情况下,如果未将x-api-key作为标题传递,则它仍会继续检查第二个条件。由于x-api-key不存在,因此它将无法读取属性的[0],因此将显示错误。

但是AND在第一次出现false条件时会停止执行,因此,如果x-api-key不是标题的一部分,则永远不会处理第二个条件。

检查javascript头中是否存在api令牌

我正在尝试测试我的API调用中是否存在API令牌。传递给的标题是-

"host": [
          {
            "key": "Host",
            "value": "mydomain"
          }
        ],
        "x-api-key": [
          {
            "key": "x-api-key",
            "value": "mykey"
          }
        ]
      }

现在我的代码看起来像

 // Check if apikey is a part of the headers 
    if ( !headers.hasOwnProperty('x-api-key') || headers['x-api-key'][0].value!="mykey") { 
        const body = 'Unauthorized';
        const response = {
            status: 401,
            statusDescription: 'Unauthorized',
            body: body,
        };
        callback(null, response);
    }

我的If案例出错,而不是如果标题完全丢失x-api-key,则不发送401

  "errorMessage": "Cannot read property '0' of undefined",

我应该如何更改条件,以便我可以同时检查键/值对,并且在缺少标题键/值的情况下也没有未定义的错误

回答如下:

尝试将您的代码更新为

 // Check if apikey is a part of the headers 
    if ( !headers.hasOwnProperty('x-api-key') || (headers.hasOwnProperty('x-api-key') && headers['x-api-key'][0].value!="mykey")) { 
        const body = 'Unauthorized';
        const response = {
            status: 401,
            statusDescription: 'Unauthorized',
            body: body,
        };
        callback(null, response);
    }

您的第一个条件将检查标题中是否存在x-api-key。第二个条件检查x-api-key的值。

OR的工作方式是,它将在第一次出现true条件时停止执行,或者将处理每个条件。因此,在您的情况下,如果未将x-api-key作为标题传递,则它仍会继续检查第二个条件。由于x-api-key不存在,因此它将无法读取属性的[0],因此将显示错误。

但是AND在第一次出现false条件时会停止执行,因此,如果x-api-key不是标题的一部分,则永远不会处理第二个条件。

发布评论

评论列表 (0)

  1. 暂无评论