![](/img/trans.png)
[英]Error when trying access parameters in AWS Python Lambda function using API Gateway
[英]Error in AWS lambda with python when testing through API Gateway
I have a basic lambda function which accepts a JSON, and transforms to XML and uploads the same to s3 bucket. 我正在通過 lambda 控制台運行測試,並且 xml 也正在生成並上傳到 s3。 當我通過 API 網關運行測試時出現問題,並且出現錯誤:
"message": "Internal server error" and Status: 502. The error also points out where is the code issue:
{"errorMessage": "'header'", "errorType": "KeyError", "stackTrace": [" File \"/var/task/sample.py\", line 21, in printMessage\n b1.text = event['header']['echoToken']\n"]}
通過 lambda 控制台運行時,相同的代碼正在工作。 下面是帶有引發錯誤的行的代碼片段:
def printMessage(event, context):
print(event)
#event = json.loads(event['header'])
root = ET.Element("Reservation")
m1 = ET.Element("header")
root.append(m1)
b1 = ET.SubElement(m1, "echoToken")
b1.text = event['header']['echoToken'] #this line throws error
我在這里遇到了類似的問題: AWS Lambda-API gateway "message": "Internal server error" (502 Bad Gateway) 。 下面是事件 object 的打印。
{'header': {'echoToken': '907f44fc-6b51-4237-8018-8a840fd87f04', 'timestamp': '2018-03-07 20:59:575Z'}, 'reservation': {'hotel': {'uuid': '3_c5f3c903-c43d-4967-88d1-79ae81d00fcb', 'code': 'TASK1', 'offset': '+06:00'}, 'reservationId': 12345, 'confirmationNumbers': [{'confirmationNumber': '12345', 'source': 'ENCORA', 'guest': 'Arturo Vargas'}, {'confirmationNumber': '67890', 'source': 'NEARSOFT', 'guest': 'Carlos Hernández'}], 'lastUpdateTimestamp': '2018-03-07 20:59:541Z', 'lastUpdateOperatorId': 'task.user'}}
根據鏈接中的解決方案,如果我嘗試這樣做: header = json.loads(event['header']) 我收到以下錯誤:
"errorMessage": "the JSON object must be str, bytes or bytearray, not dict",
這是我需要完成的任務,我嘗試了一些事情,但是在通過 API 網關和 POSTMAN 進行測試時總是失敗。 So, my question is: I don't want to change my complete code for XML transformation, and is there any way that I load the complete event object in python, so that it works both through lambda console and API gateway?
提前致謝!!
問題類似於這里討論的問題
下面的代碼將修復導致問題的行。
eventDict = json.loads(event)
b1Text = json.dumps(eventDict['header']['echoToken'])
print(b1Text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.