skip to Main Content

Using Lambda function to Get and post request. While testing it gives error
{"errorMessage": "’httpMethod’", "errorType": "KeyError", "requestId": "435e6811-acc5-4bc7-b009-377bc6178bb8", "stackTrace": [" File "/var/task/lambda_function.py", line 11, in lambda_handlern 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")
            }




The Dynamo db table has name as primary key and the json testing data is

{
    "name": "Kaira",
    "Phone Number": 98777
}

What is to be done to resolve this?

I am trying to insert the data from post method and get the data from Get method.

3

Answers


  1. Chosen as BEST ANSWER

    The get request is also fetching the data now, the detail error was decimal was not getting serialised.

    Got it worked out by importing simplejson as json


  2. DynamoDB attribute names are case sensitive – the attribute name "Name" which you write is not the as the attribute name "name" which you try to read.

    Login or Signup to reply.
  3. This error happens before you even read from DynamoDB.

    You are getting a key error while trying to parse the event object. Have a look at your event object and ensure the path of the values your are trying to resolve from it are correct.

    If that fails, share the value of event here and we can guide you better.

    I also strongly suggest wrapping API requests in try catch/except blocks

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search