使用 Lambda 函式獲取和發布請求。測驗時出現錯誤 {"errorMessage": "'httpMethod'", "errorType": "KeyError", "requestId": "435e6811-acc5-4bc7-b009-377bc6178bb8", "stackTrace": [" File "/var /task/lambda_function.py", 第 11 行,在 lambda_handler\n if event['httpMethod'] == 'GET':\n"]} 中:
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('ApigatewayDynamo')
def lambda_handler(event, context):
print("event", event)
if event['httpMethod'] == 'GET':
name = event['queryStringParameters']['name']
response = table.get_item(Key={'name': name})
print(response)
print(response['Item'])
return {
'statusCode': 200,
'body': json.dumps(response['Item'])
}
if event['httpMethod'] == 'POST':
body = json.loads(event['body'])
print('body', body)
name = body.get('name')
print('Name is ', name)
if name is None:
return {
'statusCode': 400,
'body': json.dumps("Check the payload/ method")
}
table.put_item(Item=body)
return {
'statusCode': 200,
'body': json.dumps("Name added successfully")
}
return {
'statusCode': 400,
'body': json.dumps("Check the payload/ method/ Lambda function")
}
Dynamo 資料庫表的名稱為主鍵,json 測驗資料為
{
"name": "Kaira",
"Phone Number": 98777
}
如何解決這個問題?
我正在嘗試從 post 方法插入資料并從 Get 方法獲取資料。
uj5u.com熱心網友回復:
這個錯誤發生在您甚至從 DynamoDB 讀取之前。
嘗試決議event物件時出現關鍵錯誤。查看您的event物件并確保您嘗試從中決議的值的路徑是正確的。
如果失敗,請分享event此處的價值,我們可以更好地指導您。
我還強烈建議將 API 請求包裝在 try catch/except 塊中
uj5u.com熱心網友回復:
DynamoDB 屬性名稱區分大小寫 - 您寫入的屬性名稱“Name”與您嘗試讀取的屬性名稱“name”不同。
uj5u.com熱心網友回復:
get 請求現在也在獲取資料,詳細錯誤是 decimal 沒有被序列化。
通過將 simplejson 匯入為 json 解決了這個問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/537792.html
