How to Validate Path in Proxy+ API for AWS API Gateway?

25 Views Asked by At

I have an API setup at root '/' as shown in the image.

API Gateway Proxy Integration

I have mapped this {code} in the integration request. What I actually want is to check what path is given and block the requests if a path is not mapped to my array.

Currently, I created a mapping template in integration response. Following is the code for it:

IntegrationResponses:
      - StatusCode: '200'
        ResponseTemplates:
          application/json: |
            #set($validPaths = ["oauth/v1/testing", "path2", "path3"])
            #set($inputRoot = $input.params())
            #set($response = $input.path('$'))
            #set($inputPath = $inputRoot.path.code)          
            #set($isValidPath = false)
            #foreach($path in $validPaths)
              #if($inputPath == $path)
              #set($isValidPath = true)
              #break
              #end
            #end
            #if($isValidPath)$response
            #else
            {
              "message": "Invalid path: $inputPath"
            }
            #end

But this actually hits the url and after that changes the response to invalid path. I want to block the request to hit the url if the path is not provided in the array.

How can I achieve this?

0

There are 0 best solutions below