c# - Posting from AWS-API Gateway to Lambda -
i have simple c# aws lambda function succeeds test lambda console test fails 502 (bad gateway) if called api gateway (which generated lambda trigger option) , if 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 in logs is:
wed feb 08 14:14:54 utc 2017 : endpoint response body before transformations: { "errortype": "nullreferenceexception", "errormessage": "object reference not set instance of object.", "stacktrace": [ "at blahblahmynamespace.function.functionhandler(ziprequest input, ilambdacontext context)", "at lambda_method(closure , stream , stream , contextinfo )" ] }
it seems posted object not being passed lambda input argument.
code below
// 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 null"); headers.add("testheader", "ohdear"); return new lambdaresponse { body = "fail", headers = headers, statuscode = httpstatuscode.badrequest }; } else { logger.logline($"recieved request user{input?.userid}"); logger.logline($"recieved {input?.files?.count} items zip"); headers.add("testheader", "yeah"); return new lambdaresponse { body = "hurrah", headers = headers, statuscode = httpstatuscode.ok }; } } catch (exception ex) { throw ex; } }
//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; } }
when using lambda proxy integration in api gateway, first parameter functionhandler
not body of post, api gateway-created object, let's call lambdarequest
. try these changes sample code. add:
public class lambdarequest { public string body { get; set; } }
change handler prototype to:
public lambdaresponse functionhandler(lambdarequest req, ilambdacontext context)
and inside functionhandler
add:
ziprequest input = jsonconvert.deserializeobject<ziprequest>(req.body);
the full lambdarequest object documented under input format of lambda function proxy integration in aws docs, , contains http headers, http method, query string, body, , few other things.
Comments
Post a Comment