wysguy
wysguy

Reputation: 45

How to use a Velocity variable in an AWS API Gateway Response template?

I'm unable to use a variable in an API Gateway Response

Here's a simplified example of a Response template I use to test variable usage

#set( $message= "message" )
{$message:$context.error.messageString}

This is the response that I expect

{
  message : "Unauthorized"
}

This is the actual response:

#set( = "message" )
{
    : "Unauthorized"
}

This is similar to the Hello Velocity World! example as described in the Apache Velocity Engine - User Guide

What am I doing wrong here?

Upvotes: 3

Views: 1054

Answers (2)

MikeF
MikeF

Reputation: 1018

I think

{"message":$context.error.messageString}

Would give you the results you wanted.

Upvotes: 0

ATSnipes
ATSnipes

Reputation: 66

I ran into the same issue. I needed to perform logic to map my responses correctly. Ended up stumbling on this: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-gateway-responses.responseTemplates.html which flat out says "we don't parse VTL in gateway responses".

Hope that helps!

Upvotes: 3

Related Questions