- Newest
- Most votes
- Most comments
To set the integration request parameters dynamically based on event data in an Amazon API Gateway WebSocket API, you can use the Velocity Template Language (VTL) in the integration request mapping template.
Here is an example integration request mapping template that sets the body, query, headers, and path parameters of the integration request based on the event data:
#set($inputRoot = $input.path('$')) { "body" : "$inputRoot.body", "query" : { #foreach($param in $inputRoot.query.keySet()) "$param" : "$inputRoot.query.get($param)" #if( $foreach.hasNext ),#end #end }, "headers" : { #foreach($param in $inputRoot.headers.keySet()) "$param" : "$inputRoot.headers.get($param)" #if( $foreach.hasNext ),#end #end }, "path" : { #foreach($param in $inputRoot.path.keySet()) "$param" : "$inputRoot.path.get($param)" #if( $foreach.hasNext ),#end #end } }
This template first sets the $inputRoot variable to the root of the input event data. It then sets the body, query, headers, and path parameters of the integration request to the corresponding values in the event data.
You can then use this integration request mapping template in the integration request of your WebSocket API to dynamically set the integration request parameters based on the event data.
Hi I am looking to do exactly the same thing - take a WebSocket request and transform it so that I can set the method, path, body, query and headers in the integration request before it gets send to the HTTP endpoint. The only thing I noticed by using a similar template to the one in the accepted answer is that the entire transformation gets placed into the HTTP request body. How do I set/modify the HTTP request path, headers and query without having to invoke a Lambda to do all the heavy lifting - negating the real benefit of the HTTP integration from WS -> HTTP if all that can be modified by a straight passthrough is the request body?
Anyone got this to work?
Relevant content
- asked 2 years ago
- asked a year ago
- AWS OFFICIALUpdated 6 months ago
That didn't work for me and it seems maybe that only the REST or HTTP version of Gateway supports this. I can't quite understand why this wouldn't be supported in the websocket API, but the documentation clearly leaves it out so I wasn't surprised when it didn't work.