如何将查询字符串值从AWS API Gateway传递到Lambda C#函数 [英] How to pass a querystring value from AWS API Gateway to a Lambda C# function

查看:130
本文介绍了如何将查询字符串值从AWS API Gateway传递到Lambda C#函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a C# method which I have successfully published as an AWS Lambda function. It looks like this:

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网关:

I have done the following to configure my API Gateway via the web interface:

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

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

I want to be able to call my Lambda function like this (without passing any specified headers in the request): https://xxx.execute-api.us-east-2.amazonaws.com/prod/myclass?paramA=valueA&paramB=valueB

我不确定如何获取我的querystring参数以传递给lambda函数.不管我尝试什么, request.QueryStringParameters 始终为空.

I am unsure how to get my querystring parameters to pass through to the lambda function. No matter what I try, request.QueryStringParameters is always null.

从这里开始正确的程序是什么?

What is the correct procedure from here?

推荐答案

好,我已经解决了这个问题.

Ok I've figured out the problem.

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

The APIGatewayProxyRequest is an object deserialized from the JSON passed to the Lambda function. You can see the raw JSON that is being passed to the Lambda function if you accept a JObject as the first parameter instead:

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

因此,为了填充APIGatewayProxyRequest,在主体映射模板中指定的JSON需要与APIGatewayProxyRequest的属性匹配.这里显示了一个模式示例(尽管它没有显示您需要的实际模板):

So in order to fill APIGatewayProxyRequest, the JSON specified in the Body Mapping Template needs to match the properties of APIGatewayProxyRequest. There is an example shown here of the schema (although it doesn't show the actual template that you would need): https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-input-format

但是,实际上并不需要使用API​​GatewayProxyRequest.仅接受JObject作为Lambda函数的第一个参数会更容易,然后您就可以访问所需的任何JSON.然后,您可以使用Vaibs的答案中所述的技术.

However, using APIGatewayProxyRequest is is not actually necessary. It is easier to just accept JObject as the first parameter of the Lambda function, and you then have access to whatever JSON you need. You can then use a technique like the one described in Vaibs' answer.

这篇关于如何将查询字符串值从AWS API Gateway传递到Lambda C#函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆