-1

I'm trying to learn Lambda but I'm struggling with a simple function that takes in a string as an input parameter and upper cases it:

it's written in C#.

    public class Function
    {
        public string FunctionHandler(string input, ILambdaContext context)
        {
            return input?.ToUpper();
        }
    }

I then set up a API Gateway with a get request. It has a mapping template on an integration request, like so:

{
    "input":$input.params("text")
}

I'm trying to call it at the url:

https://xxxxxxx.execute-api.xxxxxxx.amazonaws.com/Prod?test=hello

But I get an error returned:

{ "message": "Could not parse request body into json: Unexpected character (\'}\' (code 125)): expected a value\n at [Source: (byte[])\"{\n\n \"input\":\n\n}\"; line: 5, column: 2]"}

K-Dawg
  • 3,013
  • 2
  • 34
  • 52
  • Most likely the input type is a complex object. Try to either deserialize it or define the type. Check this out https://stackoverflow.com/questions/42115779/posting-from-aws-api-gateway-to-lambda – joaofs Jun 30 '19 at 13:21
  • @joaofs I think the example is a POST request not a GET which only takes url parameters. – K-Dawg Jun 30 '19 at 13:50
  • Correct, but will be a complex object with the parameters inside. Best way to check is to log the input as a string. What type of integration are you using? API Gateway? – joaofs Jun 30 '19 at 13:56
  • You can use the context to log the input message: `context.Logger.LogLine( string.Format("{0}:{1} - {2}", context.AwsRequestId, context.FunctionName, input));` – joaofs Jun 30 '19 at 13:59
  • @joaofs thanks for getting back to me. Yes API Gateway is part of AWS it allows you to create 1 standard gateway for all of your micro services. – K-Dawg Jun 30 '19 at 14:23
  • @joaofs - See: https://docs.aws.amazon.com/lambda/latest/dg/lambda-dotnet-coreclr-deployment-package.html – K-Dawg Jun 30 '19 at 14:45
  • @joaofs I should have just listened to you sooner! Making it a complex object worked! – K-Dawg Jun 30 '19 at 15:25

1 Answers1

0

As suggested by Joaofs in the comments, the fix was to make the input to my function handler a complex type. Then it started magically working.

For some reason passing a primative string worked just fine on my local box (using the SAMS test tool) but when using it from within AWS it didn't work.

Here's my new example code:

public string FunctionHandler(Employee input, ILambdaContext context)
{
   context.Logger.LogLine(string.Format("{0}:{1} - {2}", context.AwsRequestId, 
   context.FunctionName, input));
   return input.Name?.ToUpper();
}

And the employee type definition:

public class Employee
{
   public string Name { get; set; }
}
K-Dawg
  • 3,013
  • 2
  • 34
  • 52