【问题标题】:Why i am getting this {"message": "Internal server error" } from Postman为什么我从 Postman 收到此 {"message": "Internal server error" }
【发布时间】:2020-03-17 11:30:29
【问题描述】:

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

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

【问题讨论】:

  • 您不能在 Postman 中运行 Lambda。您在那里运行对特定资源的请求。内部服务器错误假定处理您的请求导致某处的系统错误。它可以是网关或 lambda 本身。查看日志。
  • 感谢您的回复。是的,我检查了日志,它返回 Status Code 200 并且数据也插入到数据库中。邮递员的回复是502 Bad gateway。有没有其他方法可以从邮递员那里获得Status Code 200 的回复?
  • @DeepakSriram 你能提供你的代码的sn-p吗?没有它,就很难猜出哪里出了问题。
  • 嘿,Deepak 支持您的答案,因为有些卑鄙的人不赞成,在提问时请尝试包含相关代码 sn-ps - 在下面的 bcosta12 答案中添加了评论!欢迎使用 Stackoverflow!
  • 感谢@bcosta12 的回答,它已正确执行

标签: amazon-web-services aws-lambda aws-api-gateway amazon-cloudwatch amazon-cloudwatchlogs


【解决方案1】:

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

尝试在您的 Lambda 中返回它:

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": "{'Test': 'Test'}",
        "headers": {
            'Content-Type': 'text/html',
        }
    }

【讨论】:

【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 1970-01-01
      • 2022-09-23
      • 2013-03-13
      • 2011-06-08
      • 2020-10-29
      • 2020-01-11
      • 2014-10-23
      • 2017-03-28
      相关资源
      最近更新 更多