Reputation: 515
I've create a very simple lambda function in VS.NET and uploaded to AWS.
namespace HelloWorld
{
public class SayHi
{
public string FunctionHandler(string name, ILambdaContext context)
{
return name?.ToUpper();
}
}
}
When I test this withing VS.NET it works and all is good.
But if I try to test this from within AWS I can't make it work. I configured a test event with the following json:
{
"name": "sample text"
}
but when I try to run this I always get the error:
{
"errorType": "JsonSerializerException",
"errorMessage": "Error converting the Lambda event JSON payload to a string. JSON strings must be quoted, for example \"Hello World\" in order to be converted to a string: The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1.",
"stackTrace": [
"at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.Deserialize[T](Stream requestStream)",
"at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
],
"cause": {
"errorType": "JsonException",
"errorMessage": "The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1.",
"stackTrace": [
"at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex)",
"at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)",
"at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)",
"at System.Text.Json.JsonSerializer.ParseCore(ReadOnlySpan`1 utf8Json, Type returnType, JsonSerializerOptions options)",
"at System.Text.Json.JsonSerializer.Deserialize[TValue](ReadOnlySpan`1 utf8Json, JsonSerializerOptions options)",
"at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.Deserialize[T](Stream requestStream)"
],
"cause": {
"errorType": "InvalidOperationException",
"errorMessage": "Cannot get the value of a token type 'StartObject' as a string.",
"stackTrace": [
"at System.Text.Json.Utf8JsonReader.GetString()",
"at System.Text.Json.Serialization.Converters.JsonConverterString.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)",
"at System.Text.Json.JsonPropertyInfoNotNullable`4.OnRead(ReadStack& state, Utf8JsonReader& reader)",
"at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)",
"at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)"
]
}
}
}
What am I missing here?
Upvotes: 2
Views: 4055
Reputation: 470
you can change the serializer for the input parameter (that I recommend not to do so). but by default, c# lambda expects the input parameter to be in JSON format.
there is a good blog post regarding Lambda in C#.
based on your sample, the input object for the lambda method should be a class, not a simple string variable, the first input should be an object like the following:
public class InputObject
{
public string Name {get;set;}
}
and your method input should change to the following:
namespace HelloWorld
{
public class SayHi
{
public string FunctionHandler(InputObject inputObject, ILambdaContext context)
{
//your logic
}
}
}
Upvotes: 2
Reputation: 12295
When testing in the Lambda Console, you need the input to be a plain string rather than the default json:
"sample text"
Otherwise the default lambda serializer isn't sure how to convert a json document to a string:
Upvotes: 1