Rentrop
Rentrop

Reputation: 21507

Return null if key does not exist in VTL / API Gateway Template

The following template returns '' if the Authorization header is not present. How can i get null instead? So return null if key is not present...

{
  "headers" : { 
     "authorization" : "$input.params().header.get('Authorization')" 
  }
}

Upvotes: 2

Views: 2742

Answers (1)

Ka Hou Ieong
Ka Hou Ieong

Reputation: 6535

You can use #if ($variable) to check if a variable is not null

#if ($variable) 
   ... do stuff here if the variable is not null
#end

In your use case, you can try to put the null check around the authorization header, like this.

{
  "headers" : {
     #if( $input.params().header.get('Authorization').toString() != "" ) 
        "authorization" : "$input.params().header.get('Authorization')" 
     #end
  }
}

Upvotes: 4

Related Questions