LambdaAnnotations | Proper use of HttpResults and [FromBody] attribute. #1569
-
Hello! I've been trying to figure out how to properly use HttpResults returning statement in the "Mock Lambda Test Tool". As a short example, I've tried this: [LambdaFunction()]
[HttpApi(LambdaHttpMethod.Get, "/")]
public IHttpResult Test(ILambdaContext context)
{
return HttpResults.Ok("Ok");
} Doing so, and returning any type of HttpResults, from the UI of Mock Lambda Test Tool I'm getting a base64 return in response. As for this case, I'm getting:
Which translate to: {
"statusCode": 200,
"headers": {
"content-type": "text/plain"
},
"Cookies": null,
"body": "Ok",
"isBase64Encoded": false
} That's correct overall, but for testing purposes, having to decode the base64 to see what I got it's not what I would want to do recurrently. Is this intended or am I missing something? If I deploy it, through browsers I can get instantly a decoded response. ─── A second thing is that I'm trying to use a [FromBody] attribute to get some data passed in the body, like this: [LambdaFunction()]
[HttpApi(LambdaHttpMethod.Post, "/")]
public IHttpResult Test([FromBody] Request request, ILambdaContext context)
{
return HttpResults.Ok("Ok");
}
public class Request
{
public string Name { get; set; }
} If through the UI Mock Lambda Test Tool I try to send {
"statusCode": 400,
"headers": {
"Content-Type": "application/json",
"x-amzn-ErrorType": "ValidationException"
},
"cookies": null,
"body": "{\u0022message\u0022: \u00221 validation error(s) detected: Value at \u0027body\u0027 failed to satisfy constraint: Value cannot be null. (Parameter \u0027json\u0027)\u0022}",
"isBase64Encoded": false
} Am I using this correctly for this case? Before trying annotations, I was using the template that comes with Powertools, there I'd just pass the type (in this case would be Request) and I wouldn't get this error. For both cases, I'm using the base template Serverless Annotation, and deleted what's inside the main Class, everything else is untouched. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
@sergio-asenjo Thanks for starting discussion. The probable reason on why in 1st scenario, Lambda Test tool displays Base64 encoded string is may be because of below generated code behind the scenes: public System.IO.Stream Test(Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest __request__, Amazon.Lambda.Core.ILambdaContext __context__)
{
// Create a scope for every request,
// this allows creating scoped dependencies without creating a scope manually.
using var scope = serviceProvider.CreateScope();
var functions = scope.ServiceProvider.GetRequiredService<Functions>();
var httpResults = functions.Test(__context__);
HttpResultSerializationOptions.ProtocolFormat serializationFormat = HttpResultSerializationOptions.ProtocolFormat.HttpApi;
HttpResultSerializationOptions.ProtocolVersion serializationVersion = HttpResultSerializationOptions.ProtocolVersion.V2;
var serializationOptions = new HttpResultSerializationOptions { Format = serializationFormat, Version = serializationVersion };
var response = httpResults.Serialize(serializationOptions);
return response;
} The IHttpResult interface declares Serialize() method whose return type is For 2nd scenario, in test tool, you need to select {
"body": "{\"Name\":\"TestName\"}",
"resource": "/{proxy+}",
"path": "/path/to/resource",
"httpMethod": "POST",
"queryStringParameters": {
"foo": "bar"
},
"pathParameters": {
"proxy": "path/to/resource"
},
"stageVariables": {
"baz": "qux"
},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept-Language": "en-US,en;q=0.8",
"Cache-Control": "max-age=0",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Host": "1234567890.execute-api.{dns_suffix}",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Custom User Agent String",
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"requestContext": {
"accountId": "123456789012",
"resourceId": "123456",
"stage": "prod",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"apiKey": null,
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "Custom User Agent String",
"user": null
},
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"apiId": "1234567890"
}
} CC @normj Thanks, @github-actions proposed-answer |
Beta Was this translation helpful? Give feedback.
-
Seeing the base64 is a bug in the mock test tool. I think what is going on is the following block of code from the tool does not have a check in the non
|
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
Seeing the base64 is a bug in the mock test tool. I think what is going on is the following block of code from the tool does not have a check in the non
Task
else block to check if the return is a stream and return it like the firstTask
if block. So theStream
is accidentally being run the theILambdaSerializer
. @ashishdhingra can you convert this into a issue tag as a bug.https://github.com/aws/aws-lambda-dotnet/blob/master/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Runtime/LambdaExecutor.cs#L184