0
#set($a = 10)
#set($b = 123)
#set($c = 456)

// If query string "q1" is not available then set $q1,$q2 to default values
#if($!input.params('q1') && $input.params('q1').empty)
  #set($q1 = $b)
  #set($q2 = $c)

// If query string "q1" available but not "q2" then add some value to $q1 and set it as $q2
#elseif($!input.params('q2') && $input.params('q2').empty)
  #set($q1 = $input.params('q1'))
  #set($q2 = $a + $q1 )

// If both query strings available then set them
#else
  #set($q1 = $input.params('q1'))
  #set($q2 = $input.params('q2'))
#end

I'm triying the above code in Integration request body mapping template. In second case where only q1 is specified as some number (let's say 10) then $q2 should be 22( 12 + 10) but it's becoming as 1210, I assume this is because those $q1 and $q2 are strings and they are getting combined.

So I tried to cast them using this answer, but I'm getting internal server error.

How can I cast string to int and them as integers?

Community
  • 1
  • 1
bravokeyl
  • 346
  • 3
  • 16
  • 28

1 Answers1

0

The solution given in the other question works for me. Try this (just the second part of if-else):

#set($a = 10)
#set($q1 = $input.params('q1'))
#set($Integer = 0)
#set($q2 = $Integer.parseInt($q1) + $a)
{
  "params" : {
    "a" : "$a",
    "q1" : "$q1",
    "q2" : "$q2"
  }
}
Balaji
  • 1,028
  • 8
  • 12