dotnet6 Minimal API using new AWS Toolkit blueprint fails on POST when trying to use object as a parameter

0

Disclaimer: new to AWS :)

So I have recently been looking at deploying a .net Core Web API which talks to DynamoDB for really simple CRUD operations on objects - I have this working perfectly in a container using dotnet6 using a previous version of the toolkit.

Was very excited to see that the latest version of the AWS toolkit for Visual Studio now fully supports dotnet6 runtime natively and so went about creating a new API based on this new blueprint (and also using the new Microsoft Minimal APIs).

My problem is that while GETs and "empty" POSTS work fine, I cannot seem to get a "POST an object" (or PUT) working - the response is always an empty "HTTP400 Bad Request".

I have tried adding the [FromBody] attribute (even though it claims to not need it) - no matter what I try, it fails with a 400 error (whether that be through Postman, Swagger or the AWS API Gateway Test facility).

Here is a the simplest POST method I could come up with:

app.MapPost("/test", (Jane jane) =>
{
    logger.Information($"test method hit with {JsonConvert.SerializeObject(jane)}");

    return Results.Ok($"works:{jane.TestString}");
});

public record Jane(string TestString);

Sending this to the endpoint:

curl -X 'POST' \
  'https://localhost:44346/test' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "testString": "string"
}'

...yields (redacted in places):

Execution log for request 435722da-4a49-4100-9342-0ff3243c28bd
Mon Mar 07 15:30:54 UTC 2022 : Starting execution for request: 435722da-4a49-4100-9342-0ff3243c28bd
Mon Mar 07 15:30:54 UTC 2022 : HTTP Method: POST, Resource Path: /
Mon Mar 07 15:30:54 UTC 2022 : Method request path: {}
Mon Mar 07 15:30:54 UTC 2022 : Method request query string: {}
Mon Mar 07 15:30:54 UTC 2022 : Method request headers: {}
Mon Mar 07 15:30:54 UTC 2022 : Method request body before transformations: {
    "testString":"stringy"
}
Mon Mar 07 15:30:54 UTC 2022 : Endpoint request URI: https://lambda.eu-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-2:057457723276:function:WL-MP-Lambda-AspNetCoreFunction-pB4FV1sakj37/invocations
Mon Mar 07 15:30:54 UTC 2022 : Endpoint request headers: {X-Amz-Date=20220307T153054Z, x-amzn-apigateway-api-id=o831bsh38b, Accept=application/json, User-Agent=AmazonAPIGateway_<snip>, Host=lambda.eu-west-2.amazonaws.com, X-Amz-Content-Sha256=a0b8c084464b647f06aa6f17e75d8918e408268a9d3623e3f5c8ac9d18745eec, X-Amzn-Trace-Id=Root=1-6226252e-4d64d963a29dce77be0b9a3a, x-amzn-lambda-integration-tag=435722da-4a49-4100-9342-0ff3243c28bd, Authorization=*********************************************************************************************************************************************************************************************************************************************************************************************************************************************96da9c, X-Amz-Source-Arn=arn:aws:execute-api:eu-west-2:057457723276:<snip>/test-invoke-stage/POST/{proxy+}, X-Amz-Security-Token=IQoJb3JpZ2luX2VjED8aCWV1LXdlc3QtMiJIMEYCIQD2yRbuuBwFielZXQHXAq1Q0TPt+4rnFXZRkCs2QR1vwAIhAMpy0xnQHhI8GBWh9QhaEXsSff6A4LIbCyXQOn+8isivKoME [TRUNCATED]
Mon Mar 07 15:30:54 UTC 2022 : Endpoint request body after transformations: {"resource":"/{proxy+}","path":"/","httpMethod":"POST","headers":null,"multiValueHeaders":null,"queryStringParameters":null,"multiValueQueryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"resourceId":"n9idsn","resourcePath":"/{proxy+}","httpMethod":"POST","extendedRequestId":"Onq_PECIrPEF4Zw=","requestTime":"07/Mar/2022:15:30:54 +0000","path":"/{proxy+}","accountId":"<snip>","protocol":"HTTP/1.1","stage":"test-invoke-stage","domainPrefix":"testPrefix","requestTimeEpoch":1646667054115,"requestId":"435722da-4a49-4100-9342-0ff3243c28bd","identity":{"cognitoIdentityPoolId":null,"cognitoIdentityId":null,"apiKey":"test-invoke-api-key","principalOrgId":null,"cognitoAuthenticationType":null,"userArn":"arn:aws:iam::057457723276:root","apiKeyId":"test-invoke-api-key-id","userAgent":"aws-internal/3 aws-sdk-java/1.12.159 Linux/5.4.172-100.336.amzn2int.x86_64 OpenJDK_64-Bit_Server_VM/25.322-b06 java/1.8.0_322 vendor/Oracle_Corporation [TRUNCATED]
Mon Mar 07 15:30:54 UTC 2022 : Sending request to https://lambda.eu-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-2:057457723276:function:WL-MP-Lambda-AspNetCoreFunction-pB4FV1sakj37/invocations
Mon Mar 07 15:30:55 UTC 2022 : Received response. Status: 200, Integration latency: 1504 ms
Mon Mar 07 15:30:55 UTC 2022 : Endpoint response headers: {Date=Mon, 07 Mar 2022 15:30:55 GMT, Content-Type=application/json, Content-Length=109, Connection=keep-alive, x-amzn-RequestId=a258fb20-9752-4689-b39a-3ee228dc9e95, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-6226252e-4d64d963a29dce77be0b9a3a;sampled=0}
Mon Mar 07 15:30:55 UTC 2022 : Endpoint response body before transformations: {"statusCode":400,"headers":{},"multiValueHeaders":{"Content-Type":[null]},"body":"","isBase64Encoded":false}
Mon Mar 07 15:30:55 UTC 2022 : Method response body after transformations: 
Mon Mar 07 15:30:55 UTC 2022 : Method response headers: {Content-Type=null, X-Amzn-Trace-Id=Root=1-6226252e-4d64d963a29dce77be0b9a3a;Sampled=0}
Mon Mar 07 15:30:55 UTC 2022 : Successfully completed execution
Mon Mar 07 15:30:55 UTC 2022 : Method completed with status: 400

Unfortunately, I can't find any examples of this sort of thing online - appreciate it if someone could point out my (no doubt) obvious mistake?! :)

I have a suspicion it's to do with the gateway->lambda->MS pipeline but have no clue where to start - have tried adding a "Jane" model to the api gateway but no joy... I've also replaced the CORS configuration (as suggested by Googling) but still it throws the maddeningly obtuse "Bad Request". Bad Dobby.

asked 2 years ago286 views
2 Answers
0

I pushed out version 1.1.0 of AMazon.Lambda.AspNetCoreServer.Hosting which is the NuGet package in your project that adds the Lambda support. This version contains a fix detecting request bodies which is required for this scenario to work. Please update your reference to 1.1.0.

AWS
Norm
answered 2 years ago
  • Thanks Norm :) I will update the GitHub once tested 👍

-1

Try using Object or JObject of Newtonsoft.Json.Linq in the place of Jane.

app.MapPost("/test", (JObject jane) => { logger.Information($"test method hit with {JsonConvert.SerializeObject(jane)}");

return Results.Ok($"works:{jane.ToObject<Jane>().TestString}"); });

AWS
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions