如何将AWS API Gateway查询字符串映射到C#AWS Lambda函数? [英] How do I map AWS API Gateway query string to C# AWS Lambda function?
问题描述
我有一个C#lambda函数,该函数是通过GET请求从API网关调用的.
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; } = "";
}
如何将发送到API网关的查询字符串参数映射到MyFunction
中的RequestModel
参数?
How do I map the query string parameters sent to API gateway to the RequestModel
parameter in MyFunction
?
我已经用参数调用了函数,但是它们似乎没有通过.是否需要等待使用C#lambda函数来实现?
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?
谢谢
克里斯
推荐答案
尝试将其放入您的RequestModel
:
public class RequestModel
{
[JsonProperty("queryStringParameters")]
public Dictionary<string, string> QueryStringParameters { get; set; }
}
然后以request.QueryStringParameters["foo"]
等访问查询字符串值.
Then access the query string values as request.QueryStringParameters["foo"]
, etc.
如果您在API网关中选中Use Lambda Proxy integration
框以查找您的资源和方法(我怀疑您这样做了,因为您已经使用statusCode
,headers
和body
字段构造了响应对象) ,相应的请求对象结构记录在
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.
我的理解是,您还可以创建自定义的有效负载映射以将请求的不同部分映射到自定义的JSON对象,但是与使用内置的Lambda代理相比,这样做需要更多的配置.
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.
这篇关于如何将AWS API Gateway查询字符串映射到C#AWS Lambda函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!