cloudformation 未在 api 网关中附加请求的路径参数
cloudformation does not attach the requested path param in api gateway
我正在尝试在 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
如果您在上面的代码中注意到我特别要求名为用户的路径参数:
RequestParameters:
method.request.path.users: true
此外,您还可以在附图中看到创建的 API 网关没有设置路径参数。
有什么想法吗?
由于需要在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'"
有两个RequestParameters
属性:一个属于方法,一个属于集成。 key 和 value 的用途略有不同,这可以理解地造成混淆。
AWS documentation 用于方法 属性(强调已添加):
The request parameters that API Gateway accepts. Specify request parameters as key-value pairs (string-to-Boolean mapping), with a source as the key and a Boolean as the value. The Boolean specifies whether a parameter is required. A source must match the format method.request.location.name, where the location is querystring, path, or header, and name is a valid, unique parameter name.
AWS documentation 用于集成 属性(强调已添加):
The request parameters that API Gateway sends with the backend request. Specify request parameters as key-value pairs (string-to-string mappings), with a destination as the key and a source as the value.
Specify the destination by using the following pattern integration.request.location.name, where location is querystring, path, or header, and name is a valid, unique parameter name.
The source must be an existing method request parameter or a static value. You must enclose static values in single quotation marks and pre-encode these values based on their destination in the request.
因此,当前接受的答案在技术上是有效的,但可能会导致静态值 true
被发送到集成参数,而不是方法参数值被传递到集成。您更有可能希望提供对方法参数的引用。
所以,为了解释键,方法 RequestParameter
键定义了 在方法请求中查找值的位置 并且集成 RequestParameter
键定义了在集成请求中放置值的位置。如果您愿意,这允许您将请求参数映射到完全不同的集成参数(例如,将请求路径参数放入集成查询字符串,将名为 foo
的请求参数更改为名为 bar
的集成参数等)
您可能也可能不需要方法参数存在,因此将方法参数布尔值设置为 true
或 false
,具体取决于您是否要强制该值必须包含在方法要求:
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
我正在尝试在 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
如果您在上面的代码中注意到我特别要求名为用户的路径参数:
RequestParameters:
method.request.path.users: true
此外,您还可以在附图中看到创建的 API 网关没有设置路径参数。 有什么想法吗?
由于需要在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'"
有两个RequestParameters
属性:一个属于方法,一个属于集成。 key 和 value 的用途略有不同,这可以理解地造成混淆。
AWS documentation 用于方法 属性(强调已添加):
The request parameters that API Gateway accepts. Specify request parameters as key-value pairs (string-to-Boolean mapping), with a source as the key and a Boolean as the value. The Boolean specifies whether a parameter is required. A source must match the format method.request.location.name, where the location is querystring, path, or header, and name is a valid, unique parameter name.
AWS documentation 用于集成 属性(强调已添加):
The request parameters that API Gateway sends with the backend request. Specify request parameters as key-value pairs (string-to-string mappings), with a destination as the key and a source as the value.
Specify the destination by using the following pattern integration.request.location.name, where location is querystring, path, or header, and name is a valid, unique parameter name.
The source must be an existing method request parameter or a static value. You must enclose static values in single quotation marks and pre-encode these values based on their destination in the request.
因此,当前接受的答案在技术上是有效的,但可能会导致静态值 true
被发送到集成参数,而不是方法参数值被传递到集成。您更有可能希望提供对方法参数的引用。
所以,为了解释键,方法 RequestParameter
键定义了 在方法请求中查找值的位置 并且集成 RequestParameter
键定义了在集成请求中放置值的位置。如果您愿意,这允许您将请求参数映射到完全不同的集成参数(例如,将请求路径参数放入集成查询字符串,将名为 foo
的请求参数更改为名为 bar
的集成参数等)
您可能也可能不需要方法参数存在,因此将方法参数布尔值设置为 true
或 false
,具体取决于您是否要强制该值必须包含在方法要求:
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