我有一個POST API網關方法,我將發送以下應用程序/ json正文以便將參數傳遞給該方法連接到的Lambda:API網關 - 正文映射模板 - 可選正文參數
{
"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文檔對這個主題有點稀疏。
感謝您的回答!我已經嘗試了許多組合:'#set($ hasAuthorizedUser =「$ context.authorizer.principalId」!=「」) #set($ hasMessage = $ input.path('$。message')!=「」 ) #設置($ 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 }'但即使所有的參數都得到一個錯誤的字符串 – RossP
雖然我知道第一個解決方案(使用引號)有效,但它要求我將字符串轉換回nu Lambda內部的人,這對我來說是不對的! – RossP
好像在授權用戶之前有額外的','嘗試:'{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 }' –