API Gateway - Body Mapping Template - optional body parameters

孤人 提交于 2021-01-27 20:10:02

问题


I have a POST API Gateway method to which I am sending the following application/json body in order to pass parameters from it to a Lambda that the method is connected to:

{ 
    "otherClientId": "12345",
    "message": "Text",
    "seconds": 0,
    "hours": 0
}

I am using the following mapping template:

#set($inputRoot = $input.path('$'))
{
  "authorizedUser": "$context.authorizer.principalId",
  "otherClientId": "$inputRoot.otherClientId",
  "message": "$inputRoot.message",
  "amount": $inputRoot.amount,
  "duration": $inputRoot.duration
}

The problem I am experiencing is that I receive a "Bad String" error when attempting to send the request without amount or duration. For some reason these parameters do not seem to be optional (but I need them to be!). I am able to miss out other parameters, like message for instance, but not the two number parameters.

Has anybody else experienced this or can somebody point out the obvious that I am probably missing? The AWS documentation is a little sparse on the subject.


回答1:


Probably, this is happening because without passing those params, your json request looks like:

#set($inputRoot = $input.path('$'))
{
  "authorizedUser": "$context.authorizer.principalId",
  "otherClientId": "$inputRoot.otherClientId",
  "message": "$inputRoot.message",
  "amount": ,
  "duration": 
}

The simplest way to fix is to wrap $inputRoot.amount and $inputRoot.duration with quotes:

#set($inputRoot = $input.path('$'))
{
   "authorizedUser": "$context.authorizer.principalId",
   "otherClientId": "$inputRoot.otherClientId",
   "message": "$inputRoot.message",
   "amount": "$inputRoot.amount",
   "duration": "$inputRoot.duration"
}

Alternatively, you can concatenate request using if condition for example:

#set($hasLimit = $input.params('limit') != "")
#set($hasOffset = $input.params('offset') != "")
#set($hasPeriod = $input.params('period') != "")
#set($hasType = $input.params('type') != "")
{
    "environment": "$input.params('environment')"
    #if($hasLimit),"limit": $input.params('limit')#end
    #if($hasOffset),"offset": $input.params('offset')#end
    #if($hasPeriod),"period": "$input.params('period')"#end
    #if($hasType),"type": "$input.params('type')"#end
}



回答2:


I think you are accidentally referencing the transformed field names via $inputRoot when you want to reference the input field names for 'seconds' and 'hours'. Not sure how you want that mapping to look for amount and duration, but $inputRoot.amount and $inputRoot.duration don't exist in the input you provided.

#set($inputRoot = $input.path('$'))
{
  "authorizedUser": "$context.authorizer.principalId",
  "otherClientId": "$inputRoot.otherClientId",
  "message": "$inputRoot.message",
  "amount": $inputRoot.seconds,
  "duration": $inputRoot.hours
}


来源:https://stackoverflow.com/questions/38647604/api-gateway-body-mapping-template-optional-body-parameters

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