Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
        };
    }
}

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

  1. Created a new API
  2. Created a new Resource with name "myclass" and path "/myclass"
  3. Created a new GET Method for the resource, using "Lambda Function" as the integration type, and pointing at my Lambda function.

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

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?

like image 534
cbp Avatar asked Dec 03 '25 07:12

cbp


2 Answers

You need to configure the url query string parameter for your request.

  1. go to API gateway

  2. click on your appropriate method i.e. GET method

  3. go to Method Execution

  4. In method execution , select URL Query String Parameter.

  5. Add query String parameter like paramA, paramB

  6. Now go to Integration Request Tab

  7. Choose Body Mapping Template, content type application/json

  8. Generate Template like below

    {
     "paramA":  "$input.params('paramA')",
     "paramB":  "$input.params('paramB')"
    }
    
  9. Accept this key value in pair in lamda function.

hope this will be helpful.

like image 174
Vaibs Avatar answered Dec 05 '25 22:12

Vaibs


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<APIGatewayProxyResponse> 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.

like image 38
cbp Avatar answered Dec 05 '25 23:12

cbp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!