How to pass a querystring value from AWS API Gateway to a Lambda C# function

后端 未结 3 1747
故里飘歌
故里飘歌 2021-01-06 13:14

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

        
3条回答
  •  清酒与你
    2021-01-06 13:28

    Ok I've figured out the problem.

    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 Test(JObject request, ILambdaContext context)
    {
        return new APIGatewayProxyResponse
        {
            Body = request.ToString(),
            StatusCode = 200
        };
    }
    

    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

    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.

提交回复
热议问题