amazon-web-servicesaws-api-gatewayvtl

How to map custom header into body request?


I'm working with AWS Api Gateway integrated with SQS.

I would like to catch a header from the request to the Api Gateway and map it to the body, so it becomes part of the message the SQS will recieve.

This is what I got

#set($headerValue = $input.params().header.get("X-Project-Id"))
#set($messagePayload = "{ \"body\": \"$input.body\", \"ProjectId\": \"$headerValue\" }" )
Action=SendMessage&MessageBody=$util.urlEncode($messagePayload)

but it fails giving the following error

Execution failed due to configuration error: Unable to transform request

What am I doing wrong and how it should be done?? I'm kind of lost with vtl as I thought this would work.


Solution

  • Finally got it with the following mapping template

    #set($inputBody = $util.parseJson($input.body))
    #set($headerValue = $input.params().header.get("X-Project-Id"))
    #set($messagePayload = '{ "body":'+$input.body+', "projectId":"'+$headerValue+'"}')
    Action=SendMessage&MessageBody=$util.urlEncode($messagePayload)