API网关端点返回200和错误

发布于 2025-02-04 19:53:36 字数 204 浏览 2 评论 0 原文

以下电话通过了,第三方服务集成到lambda中的工作原理,但该电话返回了一个错误以及200个错误。

对于您的参考,我们使用格子来处理我们的财务交易。

对导致这造成的原因有什么想法?

The following call goes through and the third party service integrated into the lambda works as expected, but the call returns an error along with a 200.

For your reference, we use Plaid to handle our financial transactions.

Any idea on what's causing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山川志 2025-02-11 19:53:36

API Gateway,如果lambda成功响应,则默认情况下,默认情况下,默认情况下,将其耦合到lambda - 总是和注意:这并不意味着lambda是成功的,只是它正确发送了响应,并且例外没有被提高到lambda处理程序之上

状态200基本上是:

< em> api 嘿。 Lambda。这是一个请求。

lambda 很酷。哦。我搞砸了。但是我要回报告诉您这件事!

api 很酷,很酷。你回应了。状态200

如果您想要其他状态,则必须使用方法和集成响应模板,映射。

如果您仅在API和Lambda之间的后端具有Lambda集成(不是代理!),那么您应该只能使用一些映射:例如(Python,可能不是那么有效,这是旧代码)) :

body_only_response_template = {
        "application/json": '''$input.path('$.body')\n'''
                            '''#if($input.path('$.statusCode').toString().contains("502"))\n'''
                            '''    #set($context.responseOverride.status = 502)\n'''
                            '''#end\n'''
                            '''#if($input.path('$.statusCode').toString().contains("400"))\n'''
                            '''    #set($context.responseOverride.status = 400)\n'''
                            '''#end'''
    }

lambda_successful_integration = apigateway.IntegrationResponse(
        status_code="200",
        content_handling=apigateway.ContentHandling.CONVERT_TO_TEXT,
        response_templates=body_only_response_template)

four_hundred_integration = apigateway.IntegrationResponse(
        status_code="400",
        content_handling=apigateway.ContentHandling.CONVERT_TO_TEXT,
        response_templates=body_only_response_template,
        selection_pattern='''.*"statusCode":.*4.*''')

five_hundred_integration = apigateway.IntegrationResponse(
        status_code="502",
        content_handling=apigateway.ContentHandling.CONVERT_TO_TEXT,
        response_templates=body_only_response_template,
        selection_pattern='''.*"statusCode":.*5.*''')

上面的CDK Python代码期望lambda这样的JSON对象:

return {
    "statusCode": 200,
    "body": "json like object stringified"

}


但是,如果API网关 - &gt; Lambda被视为“ Lambda Proxy”,然后将Lambda带到处理程序的例外,并让Lambda后端产生对Apigateway的响应。

通常,这只会导致 502:内部服务错误 apigateway的响应,但是再次使用方法响应和集成响应映射 - 您可以直接设置它。 (请注意,我从中提取的代码是XML响应,它可能不会适用 - 但是您可以看到一般的想法)

error_template = {
            "text/xml": '''#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))\n'''
                        '''$errorMessageObj.message'''
        }

500_integation = apigateway.IntegrationResponse(
            status_code="500",
            content_handling=apigateway.ContentHandling.CONVERT_TO_TEXT,
            response_templates=error_template,
            selection_pattern='''.*500.*'''  # NOTE: Lambda integrations search against "errorMessage" with this regex
        )

上面的映射期望lambda抛出例外:

except Exception as e:
     raise SomethingWentWrong(json.dumps({
            "errorType": "InternalServiceError",
            "httpStatus": 500,
            "message": "Your message, again stringified if a json-like object!"
        }))

这会导致AWS Lambda服务后端发送响应对于API网关的外观(某些东西 - 有点朦胧,有点朦胧):

{
    "errorMessage": "{\"errorType\": \"InternalServiceError\", \"httpStatus\": 500,\"message\": \"your Message here\"  }"
}
         

请注意,当它到达API时,实际错误消息将被串起。


您可以在此处获取有关映射的更多信息:

,但tl:dr:dr - &gt;如果Lambda成功返回,则具有LAMBDA默认为200的API网关,仅在特定绘制时显示另一个状态代码 - 并且使用代理集成,只有在Lambda提出一个例外时,

它完全基于API Gateway和Api Gateway和API的资源连接它的服务 +产生此响应的映射。

API gateway, when coupled to a Lambda - always returns status 200 by default if the Lambda successfully responded. And note: This does not mean the lambda was successful just that it properly sent a response and an exception did not get raised above the lambda handler

The status 200 is basically:

Api Hey. Lambda. Here is a request.

Lambda Cool. Ooooh. I messed up. But Im telling you about it with a return!

Api Cool, cool. You responded. Status 200

If you want other status you have to make use of Method and Integration Response templates, mappings.

If you have just a lambda integration (not a proxy!) on the back end between the Api and Lambda, then you should be able to just use some mapping: for example (python, and probably not that efficient, this is old code):

body_only_response_template = {
        "application/json": '''$input.path('$.body')\n'''
                            '''#if($input.path('$.statusCode').toString().contains("502"))\n'''
                            '''    #set($context.responseOverride.status = 502)\n'''
                            '''#end\n'''
                            '''#if($input.path('$.statusCode').toString().contains("400"))\n'''
                            '''    #set($context.responseOverride.status = 400)\n'''
                            '''#end'''
    }

lambda_successful_integration = apigateway.IntegrationResponse(
        status_code="200",
        content_handling=apigateway.ContentHandling.CONVERT_TO_TEXT,
        response_templates=body_only_response_template)

four_hundred_integration = apigateway.IntegrationResponse(
        status_code="400",
        content_handling=apigateway.ContentHandling.CONVERT_TO_TEXT,
        response_templates=body_only_response_template,
        selection_pattern='''.*"statusCode":.*4.*''')

five_hundred_integration = apigateway.IntegrationResponse(
        status_code="502",
        content_handling=apigateway.ContentHandling.CONVERT_TO_TEXT,
        response_templates=body_only_response_template,
        selection_pattern='''.*"statusCode":.*5.*''')

the above CDK python code is expecting the lambda to return a json object like so:

return {
    "statusCode": 200,
    "body": "json like object stringified"

}


however, if the API Gateway -> Lambda is set as "Lambda Proxy" then the only way to do this actually having the lambda raise an Exception past the handler, and letting the Lambda backend generate the response to ApiGateway.

Normally this will just result in a 502: Internal Service Error response from ApiGateway, but again - using method response and integration response mapping - you can set it directly. (do note the code I pulled this from is an XML response, which probably wont apply - but you can see the general idea)

error_template = {
            "text/xml": '''#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))\n'''
                        '''$errorMessageObj.message'''
        }

500_integation = apigateway.IntegrationResponse(
            status_code="500",
            content_handling=apigateway.ContentHandling.CONVERT_TO_TEXT,
            response_templates=error_template,
            selection_pattern='''.*500.*'''  # NOTE: Lambda integrations search against "errorMessage" with this regex
        )

the above mapping is expecting the lambda to throw an exception:

except Exception as e:
     raise SomethingWentWrong(json.dumps({
            "errorType": "InternalServiceError",
            "httpStatus": 500,
            "message": "Your message, again stringified if a json-like object!"
        }))

which causes the aws Lambda service backend to send a response to the api gateway that looks (something - its been a bit and im a bit hazy) like this:

{
    "errorMessage": "{\"errorType\": \"InternalServiceError\", \"httpStatus\": 500,\"message\": \"your Message here\"  }"
}
         

notice the actual error message is stringified when it reaches the API.


you can get more information about mappings here: https://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.html

But TL:DR -> Api Gateway with a Lambda defaults to 200 response if the lambda successfully returns, only showing another status code if you specifically map it - and with Proxy Integrations, only if the lambda raised an exception

It is entirely based on the resource connection between API Gateway and its services + the mappings that generate this response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文