Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API Gateway: How do I make querystring parameters optional in mapping template?

I can't seem to figure out how to create optional query string parameters using a mapping template within my resource's Integration Request.

My template looks like this:

{ "limit": "$input.params('limit')", "post_date":"$input.params('post_date')" } 

I'd like 'limit' & 'post_date' to be optional. This template creates a querystring that looks like this when these parameters are not provided:

/myresource?limit=undefined&

When I'm expecting:

 /myresource

The Docs don't seem to cover this. I have found some example templates in the documentation that use a bash-like syntax to provide conditional functionality. I've tried testing the following but it will NOT validate in the AWS console:

        #set($limit = $input.path('limit'))
        { 
          #if($limit)"limit": "$input.params('limit')",#end
        } 

Am I on the right track?

Thanks!

like image 304
Nick Avatar asked Sep 10 '15 20:09

Nick


1 Answers

Yes, you absolutely can do this in Api Gateway; although it does not seem to be well-documented!

In your question, you mentioned that this is a parameter; but you used input.path, which would normally refer to an element in the body of the POST request. The following should work:

#set($limit = $input.params('limit'))
{
#if($limit && $limit.length() != 0)
"limit": "$input.params('limit')"
#end
}

In terms of documentation, I found that the following page from AWS is actually pretty useful. It's tucked away in a section about mock endpoints, though:

http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-mock-integration.html

like image 169
Tom Kerswill Avatar answered Oct 11 '22 03:10

Tom Kerswill