您好,AWS 云专家,
当在任何资源上使用不受支持的 HTTP 动词时,我试图允许我的 REST API 返回 405。
我看到有一些方法可以定义GatewayResponses。
但是,我没有看到任何明显的方法来返回 405(除了将其定义为
DEFAULT_4XX
,这似乎不正确)
ExampleApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
OpenApiVersion: '3.0.1'
GatewayResponses:
DEFAULT_4XX:
StatusCode: 405
ResponseTemplates:
"application/*": '{ "message": "Method Not Allowed" }'
有人知道该怎么做吗?
一种解决方案是创建一个附加到 API 的 lambda 函数,以处理需要指示 405 的特定端点
ExampleApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
OpenApiVersion: '3.0.1'
MethodNotAllowedResponse:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs14.x
Handler: index.handler
InlineCode: |
let response;
exports.handler = (event, context, callback) => {
response = {
"statusCode": 405,
"headers": {
"Content-Type": "application/problem+json"
},
"body": JSON.stringify({
"type": "https://tools.ietf.org/html/rfc7231#section-6",
"status": 405,
"title": "Method Not Allowed",
"detail": `Method ${event.httpMethod} is not allowed on ${event.path}`
})
}
callback(null, response);
}
Events:
Televisions:
Type: Api
Properties:
Auth:
Authorizer: NONE
RestApiId: !Ref ExampleApi
Path: '/not/allowed/path'
Method: patch
这可以作为模拟集成来实现,然后您可以将其用于任何未实现/支持的方法。
集成请求映射
{
"statusCode": 405,
"message": "The invoked method is not supported on the API resource."
}