amazon-web-servicesaws-api-gatewaystring-concatenationvtlapache-velocity

Dot after Reference in Body Mapping Template (AWS API Gateway)


Is it possible to use a dot directly after a reference in API Gateway's Body Mapping Templates?

I do string concatination like this:

#set($region = "us-east-2")
#set($apiid = "$context.apiId")
#set($stage = "$context.stage")
#set($path = "search/stac")

"url": "https://$apiid.execute-api.$region.amazonaws.com/$stage/$path"

The problem is that $apiid and $region are not getting dereferenced. The result looks like this.

"url: "https:// .execute-api. .amazonaws.com/dev/search/stac

If I use the references without string concatenation they have the expected values. So the #set operation seems to be correct.

I assume it mistakes the dot after the reference as a function call. How to escape the dots in Velocity Template Language ?


Solution

  • You can use curly braces to mark the beginning and end of variable:

    "url": "https://${apiid}.execute-api.${region}.amazonaws.com/$stage/$path"
    

    If you need to explicitly separate a Velocity directive from surrounding text, it is possible to wrap it in curly braces ({ and }):