【问题标题】:API Gateway - Body Mapping Template - optional body parametersAPI Gateway - 正文映射模板 - 可选正文参数
【发布时间】:2016-12-03 12:13:08
【问题描述】:

我有一个 POST API Gateway 方法,我将以下应用程序/json 主体发送到该方法,以便将参数从它传递到该方法所连接的 Lambda:

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

我正在使用以下映射模板:

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

我遇到的问题是在尝试发送没有数量或持续时间的请求时收到“错误字符串”错误。出于某种原因,这些参数似乎不是可选的(但我需要它们!)。 我可以错过其他参数,例如消息,但不能错过两个数字参数。

有没有其他人经历过这种情况,或者有人可以指出我可能遗漏的明显情况吗? AWS 文档在这个主题上有点稀疏。

【问题讨论】:

    标签: amazon-web-services post http-post aws-lambda aws-api-gateway


    【解决方案1】:

    这可能是因为在不传递这些参数的情况下,您的 json 请求看起来像:

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

    最简单的修复方法是将$inputRoot.amount$inputRoot.duration 用引号括起来:

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

    或者,您可以使用 if 条件连接请求,例如:

    #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
    }
    

    【讨论】:

    • 感谢您的回答!我已经尝试了很多组合:#set($hasAuthorizedUser = "$context.authorizer.principalId" != "") #set($hasMessage = $input.path('$.message') != "") #set($hasAmount = $input.path('$.amount') != "") #set($hasDuration = $input.path('$.duration') != "") { #if($hasAuthorizedUser),"authorizedUser": "$context.authorizer.principalId"#end #if($hasMessage),"message": $input.path('$.message')#end #if($hasAmount),"amount": $input.path('$.amount')#end #if($hasDuration),"duration": $input.path('$.duration')#end } 但即使使用所有参数也会得到一个错误的字符串
    • 虽然我知道第一个解决方案(使用引号)有效,但它要求我将字符串转换回 Lambda 中的数字,这对我来说感觉不对!
    • 在authorizedUser之前好像有多余的,试试:{ #if($hasAuthorizedUser)"authorizedUser": "$context.authorizer.principalId"#end #if($hasMessage),"message": $input.path('$.message')#end #if($hasAmount),"amount": $input.path('$.amount')#end #if($hasDuration),"duration": $input.path('$.duration')#end }
    • 无论如何,这样做的方式,你只需要小心确保你连接正确的json对象
    • 我会尝试使用串联,看看我是否可以让它工作。你有链接到记录的地方吗?
    【解决方案2】:

    我认为当您想引用“秒”和“小时”的输入字段名称时,您不小心通过 $inputRoot 引用了转换后的字段名称。不确定您希望该映射如何查找数量和持续时间,但您提供的输入中不存在 $inputRoot.amount 和 $inputRoot.duration。

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

    【讨论】:

      猜你喜欢
      • 2017-11-30
      • 2018-01-20
      • 2018-02-03
      • 2019-06-27
      • 2020-09-28
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多