ChrisBellew
ChrisBellew

Reputation: 1174

How do I map AWS API Gateway query string to C# AWS Lambda function?

I have a C# lambda function that is called from API gateway using a GET request.

[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public ResponseModel MyFunction(RequestModel request)
{
    return new ResponseModel { body = "Hello world!" };
}

public class RequestModel 
{
    [JsonProperty("a")]
    public string A { get; set; }

    [JsonProperty("b")]
    public string B { get; set; }
}

public class ResponseModel 
{
    public int statusCode { get; set; } = 200;
    public object headers { get; set; } = new object();
    public string body { get; set; } = "";
}

How do I map the query string parameters sent to API gateway to the RequestModel parameter in MyFunction?

I have called the function with parameters but they don't seem to come through. Is there a wait to achieve this with a C# lambda function?

Thanks,

Chris

Upvotes: 2

Views: 1602

Answers (1)

phebert
phebert

Reputation: 346

Try putting this in your RequestModel:

public class RequestModel 
{
   [JsonProperty("queryStringParameters")]
   public Dictionary<string, string> QueryStringParameters { get; set; }
}

Then access the query string values as request.QueryStringParameters["foo"], etc.

If you checked the Use Lambda Proxy integration box in API Gateway for your resource and method (which I suspect you did, since you've structured your response object with the statusCode, headers, and body fields), the corresponding request object structure is documented in Input Format of a Lambda Function for Proxy Integration, buried deep in AWS's documentation. There are also other fields available like the body, headers, HTTP verb, etc.

My understanding is that you can also create a custom Payload Mapping to map different parts of the request to a custom JSON object, but doing so requires more configuration than using the built-in Lambda Proxy.

Upvotes: 3

Related Questions