首页 文章

cloudformation不会在api网关中附加请求的路径参数

提问于
浏览
0

我正在尝试在cloudformation中创建api网关 . 一切都很好,除非我指定路径参数url我可以在创建的api网关中看到它 . 这是我的cfn代码:

GetMethod:
Type: AWS::ApiGateway::Method
Properties:
  AuthorizationType: NONE
  HttpMethod: GET
  Integration:
    Type: HTTP
    IntegrationHttpMethod: GET
    Uri:
      Fn::Join:
      - ''
      - - "http://"
        - Fn::ImportValue: !Sub ${project}-${EnvironmentApp}-bn-user-endpoint-url
        - "/users"
        - "/{users}"
    IntegrationResponses:
    - StatusCode: 200
      ResponseParameters:
        method.response.header.Access-Control-Allow-Origin: "'*'"
      ResponseTemplates:
        application/json: ''
    RequestTemplates:
      application/json: ''
  RequestParameters:
    method.request.path.users: true
  ResourceId: !Ref UsersPathParam
  RestApiId:
    Ref: RestApi
  MethodResponses:
  - StatusCode: 200
    ResponseParameters:
        method.response.header.Access-Control-Allow-Origin: true

如果您在上面的代码中注意到我特意请求名为users的路径参数:

RequestParameters:
    method.request.path.users: true

此外,您可以看到创建的API网关在附加图像中没有设置路径参数 . 有任何想法吗?

supporting image1

2 回答

  • 1

    有两个 RequestParameters 属性:一个属于方法,一个属于集成 . 密钥和 Value 两者的目的略有不同,这可以理解地造成混淆 .

    AWS documentation为方法属性(强调添加):

    API Gateway接受的请求参数 . 将请求参数指定为键值对(字符串到布尔值映射),其中源为键,布尔值为值 . 布尔值指定是否需要参数 . 源必须与format.request.location.name格式匹配,其中location是querystring,path或header,name是有效的唯一参数名称 .

    AWS documentation用于集成属性(强调添加):

    API网关使用后端请求发送的请求参数 . 将请求参数指定为键值对(字符串到字符串映射),目标作为键,源作为值 . 使用以下模式integration.request.location.name指定目标,其中location是查询字符串,路径或标头,name是有效的唯一参数名称 . 源必须是现有方法请求参数或静态值 . 必须将静态值括在单引号中,并根据请求中的目标对这些值进行预编码 .

    因此,当前接受的答案在技术上是有效的,但可能会导致静态值 true 被发送到积分参数而不是传递给集成的方法参数值 . 您更有可能想要提供方法参数的引用 .

    因此,为了解释密钥,方法 RequestParameter key定义了在方法请求中找到值的位置,并且集成 RequestParameter key定义了将值放在集成请求中的位置 . 如果您愿意,这允许您将请求参数映射到完全不同的集成参数(例如,将请求路径参数放入集成查询字符串,将名为 foo 的请求参数更改为名为 bar 的集成参数等)

    您也可能需要或不需要method参数,因此请将方法参数boolean value设置为 truefalse ,具体取决于您是否要强制该值必须包含在方法请求中:

    GetMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        RestApiId: !Ref RestApi
        ResourceId: !Ref Resource
        AuthorizationType: NONE
        HttpMethod: GET
        RequestParameters:
          - method.request.path.foo: true|false
        Integration:
          Type: HTTP
          IntegrationHttpMethod: GET
          Uri: https://example.com
          RequestParameters:
            - integration.request.path.foo: method.request.path.foo
    
  • 1

    由于它需要在API的"integration request"部分中应用,因此必须在参数前加上 integration. ,如下所示:

    RequestParameters:
      integration.method.request.path.users: "'true'"
    

    另外,请注意单引号,我必须添加那些以添加字符串文字,但YMMV .

    编辑:看起来你的 RequestParameters 没有缩进到正确的位置 . 它应该在 Integration: 之下,因为它是你想要在那个级别添加的东西 .

    编辑2:我已经使用这种方法测试过它完美无缺:

    ProxyMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        ResourceId: !Ref ProxyResource
        RestApiId: !Ref RestApi
        AuthorizationType: AWS_IAM
        HttpMethod: ANY
        RequestParameters:
          method.request.path.proxy: true
        Integration:
          IntegrationHttpMethod: ANY
          Type: HTTP_PROXY
          Uri: !Sub ${BaseUrl}/{proxy}
          RequestParameters:
            integration.request.path.user: "'true'"
    

相关问题