1

I have the following code in a .Net 6 Minimal API that is running in an AWS Lambda.

in Program.cs:

app.MapPost("/{id:Guid}/test", TestHandler.Test);

In a separate file:

public static async Task<IResult> Test(TestEvent testEvent, HttpContext context, IConfiguration configuration){
...
}

I get either a 415 using test/json or a 400 using application/json. If I remove the POCO:

public static async Task<IResult> Test(HttpContext context, IConfiguration configuration)

I can desearilize the Request.Body without any issues.

string body = await new StreamReader(context.Request.Body).ReadToEndAsync();
var testEvent = JsonSerializer.Deserialize<TestEvent>(body);

But cannot get the serialization to work any other way and I'd rather not have to manually deserialize all of my POSTs. Is there any obvious reason this is not working?

And the deserialization works correctly when I run the API locally, so it has something to do with the AWS ApiGateway or the Lambda.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
cderrick
  • 76
  • 7
  • That was a typo. I corrected the code. – cderrick Apr 02 '22 at 03:06
  • Are you sending TestEvent or RegisterEvent? Have you tried using the [FromBody] attribute? – Lee Apr 02 '22 at 03:08
  • Test event, another typo. But yes, I have tried the [FromBody] attribute. – cderrick Apr 02 '22 at 03:11
  • Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. Compare https://stackoverflow.com/help/self-answer – Yunnosch Apr 02 '22 at 12:34

1 Answers1

0

It looks like AWS does something to the JSON that the System.Text.Json.Serialization doesn't handle. I switched to the Newtonsoft.Json deserializer after finding this: https://stackoverflow.com/a/69867815/1663834.

cderrick
  • 76
  • 7