【发布时间】:2022-11-20 17:29:28
【问题描述】:
使用 Lambda 函数获取和发布请求。在测试时它给出了错误 {“errorMessage”:“'httpMethod'”,“errorType”:“KeyError”,“requestId”:“435e6811-acc5-4bc7-b009-377bc6178bb8”,“stackTrace”:[“文件”/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 方法获取数据。
【问题讨论】:
标签: python amazon-web-services aws-lambda amazon-dynamodb boto3