Is it possible to use wildcards or catch-all paths in AWS API Gateway

Solution 1

As of last week, API Gateway now supports what they call “Catch-all Path Variables”.

Full details and a walk-through here: API Gateway Update – New Features Simplify API Development

Solution 2

You can create a resource with path like /{thepath+}. Plus sign is important.

Then in your lambda function you can access the value with both

  • event.path - always contains the full path
  • or event.pathParameters.thepath - contains the part defined by you. Other possible use case: define resource like /images/{imagepath+} to only match pathes with certain prefix. The variable will contain only the subpath.

You can debug all the values passed to your function with: JSON.stringify(event)

Full documentation

Solution 3

Update: As of last week, API Gateway now supports what they call “Catch-all Path Variables”. See API Gateway Update – New Features Simplify API Development.


You will need to create a resource for each level unfortunately. The reason for this is API Gateway allows you to access those params via an object.

For example: method.request.path.XXXX

So if you did just /{param} you could access that with: method.request.path.param but if you had a nested path (params with slashes), it wouldn't work. You'd also get a 404 for the entire request.

If method.request.path.param was an array instead...then it could get params by position when not named. For example method.request.path.param[] ...Named params could even be handled under there, but accessing them wouldn't really be easy. It would require using something some sort of JSON path mapping (think like what you can do with their mapping templates). Sadly this is not how it's handled in API Gateway.

I think it's ok though because this might make configuring API Gateway even more complex. However, it does also limit API Gateway and to handle this situation you will ultimately end up with a more confusing configuration anyway.

So, you can go the long way here. Create the same method for multiple resources and do something like: /{1}/{2}/{3}/{4}/{5}/{6}/{7} and so on. Then you can handle each path parameter level if need be.

IF the number of parameters is always the same, then you're a bit luckier and only need to set up a bunch of resources, but one method at the end.

source: https://forums.aws.amazon.com/thread.jspa?messageID=689700&#689700

Solution 4

Related to HTTPAPI that AWS introduced recently, $default is used a wildcard for catching all routes that don't match a defined pattern.

For more details, refer to: aws blogs

Solution 5

You can create a resource with path variable /{param}, and you can treat this as wildcard path handler.

Thanks, - Ka Hou

Share:
Admin
Author by

Admin

Updated on March 25, 2020

Comments

  • Admin
    Admin over 2 years

Related