【问题标题】:Setting path parameters in AWS API Gateway JavaScript SDK在 AWS API Gateway JavaScript SDK 中设置路径参数
【发布时间】:2020-05-18 04:12:58
【问题描述】:

我在通过 JavaScript SDK 调用 API Gateway 端点时尝试设置路径参数,但没有任何运气。看起来我要么配置错误,要么在 SDK 生成中存在错误。

我能够成功调用不带路径参数的端点,但是当我尝试传入一个用作路径参数的参数时,SDK 只是将路径参数替换为空白,我的调用失败。

例如,假设 client 是正确初始化的 API 网关客户端。我有一个名为/measurement 的端点,其子节点为/measurement/{id}。我可以直接调用两者。

client.measurementGet({},{}); - 成功调用我的/measurement 端点 client.measurementIdGet({"id": "1234"}, {}); - 浏览器调用/measurement/ 而不是/measurement/1234

查看我的 apigClient.js 的源代码,SDK 生成器似乎没有将路径参数放入它正在寻找的参数列表中。例如,我生成的measurementIdGet 方法的代码如下所示:

    apigClient.measurementIdGet = function (params, body, additionalParams) {
        if(additionalParams === undefined) { additionalParams = {}; }

        apiGateway.core.utils.assertParametersDefined(params, [], ['body']);

        var measurementIdGetRequest = {
            verb: 'get'.toUpperCase(),
            path: pathComponent + uritemplate('/measurement/{id}').expand(apiGateway.core.utils.parseParametersToObject(params, [])),
            headers: apiGateway.core.utils.parseParametersToObject(params, []),
            queryParams: apiGateway.core.utils.parseParametersToObject(params, []),
            body: body
        };

        return apiGatewayClient.makeRequest(measurementIdGetRequest, authType, additionalParams, config.apiKey);
    };

我研究了assertParametersDefinedparseParametersToObject,看起来这些方法需要一个参数列表来查找。在这两种情况下,SDK 都生成了空列表,而不是把我的路径参数放在那里。

如果我手动更新生成的文件将这两行改为

apiGateway.core.utils.assertParametersDefined(params, ['id'], ['body']);

apiGateway.core.utils.parseParametersToObject(params, ['id'])

SDK 进行了正确的调用。

我的配置中是否缺少某些内容,或者代码生成器中是否存在错误?

【问题讨论】:

  • 我也有同样的问题。这事有进一步更新吗? @梅森
  • 对我来说同样的问题。我在论坛上问过,没有得到任何答案:forums.aws.amazon.com/thread.jspa?messageID=734395。有人知道 SDK 生成器是否是开源的吗?在任何地方都找不到它
  • 对不起,这里有任何更新吗?仍然遇到这个问题

标签: amazon-web-services aws-api-gateway


【解决方案1】:

如果您像我一样使用云形成。您需要将其添加到RequestParameters

对于像这样的资源 /api/pets/{id}/attributes/{attrid} 以下代码有效

  PetsByIdAttributesByAttridGetMethod:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      RestApiId: !Ref MyApi
      ResourceId: !Ref PetsByIdAttributesByAttridResource
      HttpMethod: GET
      AuthorizationType: AWS_IAM
      RequestParameters:
        method.request.path.id : true
        method.request.path.attrid : true
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambda.Arn}/invocations

【讨论】:

    【解决方案2】:

    假设您正在导入一个 swagger 定义来创建 API,在方法级别而不是路径级别定义您的 parameters 将导致生成的 SDK 填写了 key 并且应该可以正常工作。

    {
        ...
        "/path/{to}/resource": {
            "get": {
                "parameters": [ // define here
                    "name": "to",
                    "in": "path",
                    ...
                ],
                ...
            },
            "parameters": [] // not here
    }
    

    尽管根据 Swagger 规范在路径级别定义 parameters 是正确的,并且 API Gateway 确实在创建的 API 中使用它们,但似乎 API Gateway 在某些情况下会忽略它们。

    【讨论】:

      【解决方案3】:

      这看起来像是一个不解析参数的问题。

      https://github.com/aws/chalice/issues/498enter link description here

      【讨论】:

        猜你喜欢
        • 2018-02-25
        • 1970-01-01
        • 2016-02-06
        • 1970-01-01
        • 2022-06-30
        • 2017-08-29
        • 2020-05-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多