6

我是 AWS 的新手,我{"message": "Internal server error"}在 Postman 中使用 API Gateway 运行 Lambda 函数。

我检查了CloudWatchLogs,日志中没有显示错误。但是邮递员返回{"message": "Internal server error"}此错误。

4

3 回答 3

8

当您没有返回正确的 API Gateway 格式时,就会发生这种情况。

尝试在您的 Lambda 中返回它:

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": "{'Test': 'Test'}",
        "headers": {
            'Content-Type': 'text/html',
        }
    }
于 2019-11-21T14:12:35.390 回答
2

根据您提供的日志消息,它看起来像是来自您的 Lambda 函数的日志。我建议您在 API Gateway 端启用日志记录功能,或者您可以在 API Gateway 控制台上使用测试调用功能。他们都能够帮助您调试您的 API。

以下是可能有助于您诊断问题的常见问题。1.它没有权限允许API Gateway调用你的Lambda函数。2. 它被设置为您的 Lambda 函数的 AWS 服务代理,您的 Lambda 函数的响应没有以正确的格式返回响应。

参考:https ://forums.aws.amazon.com/thread.jspa?messageID=916452

于 2019-11-21T14:15:58.467 回答
0

该错误可能是由不正确的引号引起的。

A. 在 Postman 测试中产生相同错误的示例:

 1. def lambda_handler(event, context):
      return {
        'statusCode': 200,
        "body": {"one": 1000}
      }
 2. def lambda_handler(event, context):
      return {
        'statusCode': 200,
        "body": "{"one": 1000}"
      }

B. 不产生错误的例子:

3. def lambda_handler(event, context):
     return {
       'statusCode': 200,
       "body": "{'one': 1000}"
     }


4. def lambda_handler(event, context):
     return {
       'statusCode': 200,
       "body": '{"one": 1000}'
     }

所以,在“body”之后使用的引号类型:是这种情况下错误的原因。请注意,虽然 Amazon lambda 控制台不会产生示例 1 的错误,但 Postman 说{ "message": "Internal server error" }

于 2020-10-04T07:09:04.090 回答