Getting {"errorMessage": "'httpMethod'", "errorType": "KeyError" while testing Aws Lambda function

问题描述 投票:0回答:1

我正在创建无服务器架构。在那里我试图通过 api 网关打开“index.html”页面。但是在测试 Lambda 函数以发布和获取请求时。我收到错误 { "errorMessage": "'http方法'", "errorType": "KeyError", “requestId”:“edaf3570-7804-43ac-a4c8-3e6ea0aa1c40”, “堆栈跟踪”: [ “文件”/var/task/lambda_function.py,第 5 行,lambda_handler mypage = page_router(event['httpmethod'], event['quaryStringParameters'], event['formbody']) “ ] `

import json
import os, re, base64
import boto3
def lambda_handler(event, context):
    mypage = page_router(event['httpmethod'], event['quaryStringParameters'], event['formbody'])
    return mypage
    # print("Inside lambda function")
    # print(event)

def page_router(httpmethod, quaryStringParameters, formbody):
    if httpmethod == 'POST':
        htmlFile = open('index.html', 'w')
        htmlContent = htmlFile.write()
        insert_record(formbody)
        return {
        'statusCode': 200, 
        'headers': {"Content-Type":"text/html"},
        'body': htmlContent,
        }
    
    if httpmethod == 'GET':
        # insert_record(formbody)
        htmlFile = open('confirm.html', 'r')
        htmlContent = htmlFile.read()
        return {
        'statusCode': 200, 
        'headers': {"Content-Type":"text/html"},
        'body': htmlContent,
        # 'isBase64Encoded': False
        }

def insert_record(formbody):
    formbody = formbody.replace("=", "' : '")
    formbody = formbody.replace("&", "' , '")
    formbody = "INSERT INTO dynamodb value {'" + formbody +  "'}"

    client = boto3.client('studentinfo')
    client.execute_statement(Statement = formbody)

我正在尝试通过“POST”方法将数据插入“index.html”页面,并且数据保存在 Dynamodb 中。通过'post'方法将数据插入“index.html”并单击'index.html'中的'submit'按钮后,它通过'GET'方法打开'confirm.html'页面。

如何解决这个问题以及我的 json 事件应该是什么?

python json amazon-web-services aws-lambda aws-api-gateway
1个回答
0
投票

事件中的一个键(httpmethod、queryStringParameters 和 formbody)或所有键不存在。

在方法的开头打印(事件),并在日志中查看错误。

© www.soinside.com 2019 - 2024. All rights reserved.