简体   繁体   English

AWS Lambda-API 网关“消息”:“内部服务器错误”(502 错误网关)

[英]AWS Lambda-API gateway "message": "Internal server error" (502 Bad Gateway)

I created a simple AWS Lambda function to add two numbers using python 3.6.我创建了一个简单的 AWS Lambda 函数来使用 python 3.6 将两个数字相加。 It reads val1 & val2 values in json body.它读取 json 正文中的val1val2值。 When I tested lambda function in lambda console it works fine.当我在 lambda 控制台中测试 lambda 函数时,它工作正常。 But when I call lambda function by a POST request through AWS API gateway using POSTMAN, it responses with "message": "Internal server error" (502 Bad Gateway).但是,当我使用 POSTMAN 通过 AWS API 网关通过 POST 请求调用 lambda 函数时,它响应“消息”:“内部服务器错误”(502 错误网关)。 Can anyone help me with this error?谁能帮我解决这个错误?

Lambda function Lambda 函数

import json
def lambda_handler(event, context):
    # TODO implement
    val1 = int(event['val1'])
    val2 = int(event['val2'])
    val3 = val1 + val2
    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(val3)
    }

JSON body JSON 正文

{
    "val1": "3",
    "val2": "5"
}

This error occurs due to the behaviour of the event object (python dictionary).发生此错误是由于事件对象(python 字典)的行为。 When you test lambda function in lambda console JSON body will directly passed to the event object.当您在 lambda 控制台中测试 lambda 函数时,JSON 主体将直接传递给事件对象。 But when you try it through API gateway, not only event object is the request payload but also body attribute is set as a string.但是当您通过 API 网关尝试时,不仅事件对象是请求负载,而且正文属性也被设置为字符串。

For example your JSON body will be like this in event object例如,您的 JSON 正文在事件对象中将是这样的

body: "{\n    \"val1\": \"3\",\n    \"val2\": \"5\"\n}"

To resolve this error try json.loads() method to convert body string to json.要解决此错误,请尝试使用 json.loads() 方法将正文字符串转换为 json。

import json
def lambda_handler(event, context):
    # TODO implement
    try:
        event = json.loads(event['body'])
        val1 = int(event['val1'])
        val2 = int(event['val2'])
        val3 = val1 + val2
    except:
        val3 = 'request error'
    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(val3)
    }

I ended up here with the same error only I was using Serverless and Node.我最终遇到了同样的错误,只是我使用的是无服务器和 Node.js。 I was returning an HTTP response without using stringify on the body.我在没有在正文上使用 stringify 的情况下返回了一个 HTTP 响应。 As seen in the bad example below:如下面的坏例子所示:

Bad:坏的:

return {
            statusCode,
            body: body.message,
            headers: {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Credentials": true,
            },
        };
    };
}

Good:好的:

return {
            statusCode,
            body: JSON.stringify(body.message),
            headers: {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Credentials": true,
            },
        };
    };
}

I hope this helps others who may run into the 502 error.我希望这可以帮助其他可能遇到 502 错误的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM