Home > Blockchain >  How to allow API Gateway to return a 405 response
How to allow API Gateway to return a 405 response

Time:10-06

Hello AWS Cloud Gurus,

I am trying to allow my REST API to return a 405 when an unsupported HTTP verb is used on any resource.

I see there are ways to define GatewayResponses.

However, I don't see any obvious approach to return a 405 (other than to define it as the DEFAULT_4XX which seems not-obvious)

Does anyone know how to do this?

CodePudding user response:

This is an example "solution" but it doesn't feel like the correct solution in my opinion.

  ExampleApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      OpenApiVersion: '3.0.1'
      GatewayResponses:
        DEFAULT_4XX:
          StatusCode: 405
          ResponseTemplates:
            "application/*": '{ "message": "Method Not Allowed" }'

CodePudding user response:

This can be implemented as a mock integration that you then use for any methods that are not implemented/supported.

Integration request mapping

{
  "statusCode": 405,
  "message": "The invoked method is not supported on the API resource."
}
  • Related