aws/aws-lambda-dotnet

Minimal API default content-type for bad request is application/octect-stream , chrome returns ERR_INVALID_RESPONSE

sebastienlabine opened this issue · 2 comments

Describe the bug

When using the minimal api extension method
builder.Services.AddAWSLambdaHosting(LambdaEventSource.ApplicationLoadBalancer)
the response from our lambda when returning a bad request return (NotFound(), BadRequest(), Unauthorized()) has the content-type application/octect-stream which should be application/json.

This creates a ERR_INVALID_RESPONSE on chrome

image
image

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

The content type returned should be application/json

Current Behavior

The content type returned is application/octect-stream

Reproduction Steps

  1. Create a minimal API setup and add the AddAWSLambdaHosting(LambdaEventSource.ApplicationLoadBalancer)
  2. Create a controller and return a 400,500 status code.
  3. Test locally and observe the correct headers
  4. Deploy the lambda and observe the wrong content-type

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Amazon.Lambda.AspNetCoreServer.Hosting1.7.3

Targeted .NET Platform

.NET Core 8

Operating System and version

Windows 10

@sebastienlabine Good afternoon. Somehow I'm unable to reproduce the issue using the dotnet new serverless.AspNetCoreMinimalAPI template and adding below Error route to the generated CalculatorController:
Controllers\CalculatorController.cs

using Microsoft.AspNetCore.Mvc;

namespace MinimalApiOnLambda.Controllers;

[ApiController]
[Route("[controller]")]
public class CalculatorController : ControllerBase
{
    private readonly ILogger<CalculatorController> _logger;

    public CalculatorController(ILogger<CalculatorController> logger)
    {
        _logger = logger;
    }

...
...

    [HttpGet("error")]
    public IActionResult Error()
    {
        return StatusCode(500);
    }
}

Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();

// Add AWS Lambda support. When application is run in Lambda Kestrel is swapped out as the web server with Amazon.Lambda.AspNetCoreServer. This
// package will act as the webserver translating request and responses between the Lambda event source and ASP.NET Core.
builder.Services.AddAWSLambdaHosting(LambdaEventSource.ApplicationLoadBalancer);

var app = builder.Build();

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.MapGet("/", () => "Welcome to running ASP.NET Core Minimal API on AWS Lambda");

app.Run();

Deploying above code still creates a RestApi in API Gateway. Followed instructions at Lambda functions as targets for Application Load Balancers to configure Serverless Web API Lambda function with ALB (it basically mentions creating ALB and adding Lambda ALB trigger). After deployment, we would need to add security group inbound rule to allow HTTP 80 or HTTPS 443 ports.

Requesting Error endpoint using ALB DNS name (e.g. http://testminimalapialb-130218691.us-east-2.elb.amazonaws.com/calculator/error) gives the below response:

{"type":"https://tools.ietf.org/html/rfc9110#section-15.6.1","title":"An error occurred while processing your request.","status":500,"traceId":"00-36059fcd25a659e4e3509775dce94012-f18854f5cf1d8224-00"}

Screenshot 2025-01-03 at 3 04 42 PM
Notice that the content type is application/problem+json; charset=utf-8.

Please share the minimal reproducible code solution along with steps to reproduce the issue.

Thanks,
Ashish

This issue has not received a response in 5 days. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.