我正在尝试在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 网关没有设置路径参数。 有什么想法吗?
最佳答案
有两个 RequestParameters
属性:一个属于方法,一个属于集成。 键和值的用途略有不同,这可能会造成困惑,这是可以理解的。
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
的请求参数更改为名为 栏
等)
您也可能需要或不需要方法参数存在,因此根据您是否要强制执行该值,将方法参数 bool 值设置为 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
关于amazon-web-services - cloudformation 未在 api 网关中附加请求的路径参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48770324/