如何将HTTP API或REST API与Lambda(不更新的能力)提供给HTTP API或REST API?

发布于 2025-01-30 05:20:09 字数 952 浏览 2 评论 0原文

因此,我具有以下代码的lambda功能,该代码由API触发(AWS API网关)。

import json

def lambda_handler(event, context):
    testString = "foo"
    if "f" in testString:
        return {
            "statusCode": 200,
            "body": json.dumps(testString)
        }

我用HTTP API和REST API对其进行了测试。由于条件返回true,在这两种情况下,输出都是“ foo”。但是,如果testString突然更改为“ Goo”,并且条件返回false,会发生什么?我希望输出保持原样(未更新),因此它仍然是“ foo”。但是发生这种情况时,HTTP API输出null,而REST API输出{“ message”:“ Internal Server Error”}

也许我只需要在下面的代码中弄清楚缺少的作品:

import json

def lambda_handler(event, context):
    testString = "goo"
    if "f" in testString:
        return {
            "statusCode": 200,
            "body": json.dumps(testString)
        }
    else:
        #missing piece: make output not change

这可能是第一次,我尝试了“创建” APIS TBH。我想念什么?

So I have a Lambda Function with the code below, which is triggered by an API (AWS API Gateway).

import json

def lambda_handler(event, context):
    testString = "foo"
    if "f" in testString:
        return {
            "statusCode": 200,
            "body": json.dumps(testString)
        }

I tested it with both a HTTP API and a REST API. Because the condition returns True, in both cases the output is "foo" as it should be. But what happens if testString suddenly changes to "goo", and the condition returns False? I would like the output to remain as it previously was (not update), so it remains "foo". But when this happens, the HTTP API outputs null, and the REST API outputs {"message": "Internal server error"}.

Maybe I just need to figure out the missing piece in the code below:

import json

def lambda_handler(event, context):
    testString = "goo"
    if "f" in testString:
        return {
            "statusCode": 200,
            "body": json.dumps(testString)
        }
    else:
        #missing piece: make output not change

This is probably the first time, I have tried "creating" APIs tbh. What am I missing?

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

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

发布评论

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

评论(1

泪痕残 2025-02-06 05:20:09

我希望输出t如前所述(不更新),因此它仍然是“ foo”

您不能仅使用lambda函数来完成此操作。您必须存储以前的输出外部,例如dynamodb。然后,您的功能将始终能够查找最后一个正确的结果,然后将其返回,而不是随机错误消息或不正确的答案。

I would like the output t remain as it previously was (not update), so it remains "foo"

You can't do this with just only a lambda function. You have to store your previous outputs externally, e.g. in a DynamoDB. Then your function will always be able to look up the last correct result and return it instead some random error message or incorrect answer.

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