【问题标题】:Getting {"errorMessage": "'httpMethod'", "errorType": "KeyError"获取 {\"errorMessage\": \"\'httpMethod\'\", \"errorType\": \"KeyError\"
【发布时间】: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


    【解决方案1】:

    DynamoDB 属性名称区分大小写 - 您写入的属性名称“Name”与您尝试读取的属性名称“name”不同。

    【讨论】:

    • 即使我到处都将名称更改为“名称”,我仍然会遇到同样的错误
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2022-01-25
    • 2016-02-22
    相关资源
    最近更新 更多