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

血红的双手。 提交于 2020-05-14 17:56:06

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/32511087/aws-api-gateway-how-do-i-make-querystring-parameters-optional-in-mapping-templa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!