Home > Net >  Getting {"errorMessage": "'httpMethod'", "errorType": "
Getting {"errorMessage": "'httpMethod'", "errorType": "

Time:11-20

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_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")
            }




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.

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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

  • Related