【问题标题】:How to pass a querystring value from AWS API Gateway to a Lambda C# function如何将查询字符串值从 AWS API Gateway 传递到 Lambda C# 函数
【发布时间】:2023-04-02 20:31:01
【问题描述】:

我有一个已成功发布为 AWS Lambda 函数的 C# 方法。它看起来像这样:

public class MyClass
{
    public async Task<APIGatewayProxyResponse> Test(APIGatewayProxyRequest request, ILambdaContext context)
    {
        return new APIGatewayProxyResponse
        {
            Body = "Body: " + request.Body
                   + Environment.NewLine
                   + "Querystring: " + (request.QueryStringParameters == null ? "null" : string.Join(",", request.QueryStringParameters.Keys)),
            StatusCode = 200
        };
    }
}

我已通过 Web 界面执行以下操作来配置我的 API 网关:

  1. 创建了新的 API
  2. 创建了一个名为“myclass”和路径“/myclass”的新资源
  3. 为资源创建了一个新的 GET 方法,使用“Lambda 函数”作为集成类型,并指向我的 Lambda 函数。

我希望能够像这样调用我的 Lambda 函数(无需在请求中传递任何指定的标头): https://xxx.execute-api.us-east-2.amazonaws.com/prod/myclass?paramA=valueA&paramB=valueB

我不确定如何让我的查询字符串参数传递给 lambda 函数。无论我尝试什么,request.QueryStringParameters 始终为空。

这里的正确程序是什么?

【问题讨论】:

    标签: c# aws-lambda aws-api-gateway


    【解决方案1】:

    您需要为您的请求配置url查询字符串参数。

    1. 转到 API 网关

    2. 点击合适的方法,即GET方法

    3. 转到方法执行

    4. 在方法执行中,选择URL查询字符串参数。

    5. 添加查询字符串参数,如paramA、paramB

    6. 现在转到集成请求选项卡

    7. 选择正文映射模板,内容类型application/json

    8. 生成如下模板

      {
       "paramA":  "$input.params('paramA')",
       "paramB":  "$input.params('paramB')"
      }
      
    9. 在 lamda 函数中成对接受这个键值。

    希望这会有所帮助。

    【讨论】:

    • 嗨,对不起,这不起作用:在 Labmda 函数中 request.QueryStringParameters 仍然为空。你以前用过 C# 吗?
    • 这是正确的答案。顺便说一句,如果请求是 POST 或 GET,我如何检查 lambda 内部?
    • 您可以在aws API的集成请求中使用"http_method": "$context.httpMethod"。然后您可以在aws lambda中访问事件中的新属性。
    【解决方案2】:

    好的,我已经解决了问题。

    APIGatewayProxyRequest 是从传递给 Lambda 函数的 JSON 反序列化的对象。如果您接受 JObject 作为第一个参数,则可以看到传递给 Lambda 函数的原始 JSON:

    public async Task<APIGatewayProxyResponse> Test(JObject request, ILambdaContext context)
    {
        return new APIGatewayProxyResponse
        {
            Body = request.ToString(),
            StatusCode = 200
        };
    }
    

    所以为了填充 APIGatewayProxyRequest,Body Mapping Template 中指定的 JSON 需要匹配 APIGatewayProxyRequest 的属性。这里显示了一个模式示例(尽管它没有显示您需要的实际模板):https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-input-format

    然而,使用 APIGatewayProxyRequest 实际上并不是必需的。接受 JObject 作为 Lambda 函数的第一个参数会更容易,然后您就可以访问所需的任何 JSON。然后,您可以使用 Vaibs 的回答中描述的技术。

    【讨论】:

      【解决方案3】:

      请使用“$input.params('YourQueryStringKey')”。

      您可以在 API 网关集成响应中创建正文映射模板并尝试“$input.params('YourQueryStringKey')”或直接在 Lambda 函数中。

      【讨论】:

      • 您好,这是我尝试过的方法之一 - 您能否更具体地说明我如何将它包含在我的身体映射模板或 Lambda 函数中?
      • 嗯那个链接已经过时了——我相信 AWS 接口在 2016 年写完这篇文章后发生了变化。不幸的是,遵循这些说明会导致相同的 NullReferenceException。
      • 哦,对不起,伙计。我会在今天某个时候这样做并分享屏幕截图。
      猜你喜欢
      • 2017-11-14
      • 2017-05-08
      • 2015-09-28
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 2015-11-10
      • 2016-11-18
      相关资源
      最近更新 更多