gpt4 book ai didi

aws-lambda - lambda - {"message": "Internal server error"}

转载 作者:行者123 更新时间:2023-12-02 01:07:18 25 4
gpt4 key购买 nike

给我谜语吧!

为什么是 lambda?

curl -X POST -H "Content-Type: application/json" -d '{"first_name":"ABCDE", "last_name": "john"}' https://8.execute-api.us-east-1.amazonaws.com/prod/HelloPython

{"message": "Internal server error"}

它在 lambda 测试中有效!当然,我是新手,但我如何找出原因?我在云指标中所需要的只是 500 个错误的计数。但我想知道为什么。我正在做一个简单的 hello world。

import json
def respond(err, res=None):
return {
'statusCode': '400' if err else '200',
'body': err.message if err else json.dumps(res),
'headers': {
'Content-Type': 'application/json',
},
}

def my_handler(event, context):


message = 'Hello {} {}!'.format(event['first_name'],
event['last_name'])
res = {'message' : message }
return respond(None, res)

更新 - 以下是日志:

'first_name': KeyError
Traceback (most recent call last):
File "/var/task/hello_python.py", line 68, in my_handler
message = 'Hello
{}
{}
!'.format(event['first_name'],
KeyError: 'first_name'

我很困惑。事件是什么类型的对象?如果我需要一个字符串,是否需要使用 json.loads(event) 来获取字典对象?

最佳答案

我通常不喜欢回答我自己的问题,但作为 lambda 的新手,我希望这对某人有所帮助。

1) 事件数据如下所示:

{
u 'body': u '{"first_name":"ABCDE", "last_name": "john"}', u 'resource': u '/HelloPython', u 'requestContext': {
u 'resourceId': u 'kl9',
u 'apiId': u '81s0q',
u 'resourcePath': u '/HelloPython',
u 'httpMethod': u 'POST',
u 'requestId': u 'e1c72ee6-7-9059-1bd746727831',
u 'path': u '/prod/HelloPython',
u 'accountId': u 'cccc',
u 'identity': {
u 'apiKey': u '',
u 'userArn': None,
u 'cognitoAuthenticationType': None,
u 'accessKey': None,
u 'caller': None,
u 'userAgent': u 'curl/7.54.0',
u 'user': None,
u 'cognitoIdentityPoolId': None,
u 'cognitoIdentityId': None,
u 'cognitoAuthenticationProvider': None,
u 'sourceIp': u 'ccccc',
u 'accountId': None
},
u 'stage': u 'prod'
}, u 'queryStringParameters': None, u 'httpMethod': u 'POST', u 'pathParameters': None, u 'headers': {
u 'Content-Type': u 'application/json',
u 'Via': u '1.1 07c960c5d.cloudfront.net (CloudFront)',
u 'CloudFront-Is-Desktop-Viewer': u 'true',
u 'CloudFront-Is-SmartTV-Viewer': u 'false',
u 'CloudFront-Forwarded-Proto': u 'https',
u 'X-Forwarded-For': u 'xxx, cccc',
u 'CloudFront-Viewer-Country': u 'US',
u 'Accept': u '*/*',
u 'User-Agent': u 'curl/7.54.0',
u 'X-Amzn-Trace-Id': u 'Root=1-59f4e74a9fecde',
u 'Host': u '81s0q.execute-api.us-east-1.amazonaws.com',
u 'X-Forwarded-Proto': u 'https',
u 'X-Amz-Cf-Id': u 'GXnihIB370EYguOCiGYEA==',
u 'CloudFront-Is-Tablet-Viewer': u 'false',
u 'X-Forwarded-Port': u '443',
u 'CloudFront-Is-Mobile-Viewer': u 'false'
}, u 'stageVariables': None, u 'path': u '/HelloPython', u 'isBase64Encoded': False
}

2) 数据在主体中,然后必须从 json 加载:

test = json.loads(event['body'])  
message = "Dude %s %s" % (test['first_name'], test['last_name'])

Curl 现在可以使用了

关于aws-lambda - lambda - {"message": "Internal server error"},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46986717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com