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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With