amazon-web-servicesaws-api-gatewayopenapivelocityvtl

VTL adds whitespaces to string


I am using mapping template in AWS API Gateway methods Integration response. I found that it adds unwanted whitespaces to the return string. I have read Velocity references and tried all trimming and replacing I could think of. However there still seems to be 3 whitespaces before text. My actual code is more complicated but this is a short example of the problem.

#set ($test = "Foo bar")
##return that
$test.toString().trim()

Solution

  • Most probably, the extra spaces come from elsewhere in the template.

    First, you can try to add some extra characters to understand where the spaces come from:

    <#set ($test = "Foo bar")>
    @@##return that
    <$test.toString().trim()>
    

    and look where the spaces do fall.

    Also, you can try to comment end lines to see if it changes something:

    #set ($test = "Foo bar")##
    ##return that
    $test.toString().trim()##