从AWS-API网关发布到Lambda [英] Posting from AWS-API Gateway to Lambda
问题描述
我有一个简单的C#Aws Lambda函数,该函数可以通过Lambda控制台测试成功进行测试,但是如果从API网关(我是从Lambda触发选项生成的)中调用的,则失败并显示502(错误网关)使用邮递员.(此初始功能具有开放访问权限(无安全性))
I have a simple C# Aws Lambda function which succeeds to a test from the Lambda console test but fails with a 502 (Bad Gateway) if called from the API Gateway (which i generated from the Lambda trigger option) and also if I use postman.(this initial function has open access (no security))
// request header
Content-Type: application/json
// request body
{
"userid":22,
"files":["File1","File2","File3","File4"]
}
我在日志中得到的错误是:
The error I get in the logs is:
Wed Feb 08 14:14:54 UTC 2017 : Endpoint response body before transformations: {
"errorType": "NullReferenceException",
"errorMessage": "Object reference not set to an instance of an object.",
"stackTrace": [
"at blahblahmynamespace.Function.FunctionHandler(ZipRequest input, ILambdaContext context)",
"at lambda_method(Closure , Stream , Stream , ContextInfo )"
]
}
似乎发布的对象没有传递给lambda输入参数.
It seems like the posted object is not being passed to the lambda input argument.
下面的代码
// Lambda function
public LambdaResponse FunctionHandler(ZipRequest input, ILambdaContext context)
{
try
{
var logger = context.Logger;
var headers = new Dictionary<string, string>();
if (input == null || input.files.Count == 0)
{
logger.LogLine($"input was null");
headers.Add("testheader", "ohdear");
return new LambdaResponse { body = "fail", headers = headers, statusCode = HttpStatusCode.BadRequest };
}
else
{
logger.LogLine($"recieved request from user{input?.userid}");
logger.LogLine($"recieved {input?.files?.Count} items to zip");
headers.Add("testheader", "yeah");
return new LambdaResponse { body = "hurrah", headers = headers, statusCode = HttpStatusCode.OK };
}
}
catch (Exception ex)
{
throw ex;
}
}
//Lambda响应/ZipRequest类
//Lambda response/ZipRequest class
public class LambdaResponse
{
public HttpStatusCode statusCode { get; set; }
public Dictionary<string, string> headers { get; set; }
public string body { get; set; }
}
public class ZipRequest
{
public int userid { get; set; }
public IList<string> files { get; set; }
}
推荐答案
在API Gateway中使用Lambda代理集成时,FunctionHandler
的第一个参数不是POST的主体,而是另一个API Gateway创建的对象,我们称之为LambdaRequest
.尝试对示例代码进行这些更改.添加:
When using Lambda Proxy Integration in API Gateway, the first parameter to your FunctionHandler
is not the body of your POST, but is another API Gateway-created object, which let's call LambdaRequest
. Try these changes to your sample code. Add:
public class LambdaRequest
{
public string body { get; set; }
}
将处理程序原型更改为:
Change your handler prototype to:
public LambdaResponse FunctionHandler(LambdaRequest req, ILambdaContext context)
并在FunctionHandler
内添加:
ZipRequest input = JsonConvert.DeserializeObject<ZipRequest>(req.Body);
The full LambdaRequest object is documented under Input Format of a Lambda Function for Proxy Integration in the AWS docs, and contains HTTP headers, the HTTP method, the query string, the body, and a few other things.
这篇关于从AWS-API网关发布到Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!