Explore Courses Blog Tutorials Interview Questions
0 votes
in AWS by (19.1k points)

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





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 )"



It seems like the posted object is not being passed to the lambda input argument.

Code below 

// Lambda function

     public LambdaResponse FunctionHandler(ZipRequest input, ILambdaContext context)




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




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


1 Answer

0 votes
by (44.4k points)

The first parameter to your FunctionHandler is not the body of your POST, but it is another API Gateway-created object when you use Lambda Proxy Integration. Try this:

public class LambdaRequest


   public string body { get; set; }



Change your handler prototype to:

public LambdaResponse FunctionHandler(LambdaRequest req, ILambdaContext context)


And inside FunctionHandler add:

ZipRequest input = JsonConvert.DeserializeObject<ZipRequest>(req.Body);


Look at this documentation for information - Set up a Proxy Integration with a Proxy Resource

Browse Categories