C
C#3mo ago
Natro

Receiving 400 on production, local env works fine

Hey, I have same ASP.NET Core app running both on my localhost and on production (docker). The only difference between the two are environment variables (aspnet env - release for prod and development for localhost). When I run following powershell to call one of my endpoints:
$headers = @{
'accept' = '*/*'
'Content-Type' = 'application/json'
}
$body = @{
'nonce' = '7ba0879e-476e-4962-85b9-9d36be449d21'
'integrationType' = 'Slack'
}
Invoke-RestMethod -Uri 'https://localhost:8084/Integration' -Method 'POST' -Headers $headers -Body ($body | ConvertTo-Json)
Invoke-RestMethod -Uri 'https://{obfuscated}/Integration' -Method 'POST' -Headers $headers -Body ($body | ConvertTo-Json)
$headers = @{
'accept' = '*/*'
'Content-Type' = 'application/json'
}
$body = @{
'nonce' = '7ba0879e-476e-4962-85b9-9d36be449d21'
'integrationType' = 'Slack'
}
Invoke-RestMethod -Uri 'https://localhost:8084/Integration' -Method 'POST' -Headers $headers -Body ($body | ConvertTo-Json)
Invoke-RestMethod -Uri 'https://{obfuscated}/Integration' -Method 'POST' -Headers $headers -Body ($body | ConvertTo-Json)
The localhost goes through and production throws:
Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.ProblemDetails'.
Executed action {obfuscated}.IntegrationController.MatchIdentityAsync ({obfuscated}) in 26.5296ms
Executed endpoint '{obfuscated}.Controllers.IntegrationController.MatchIdentityAsync ({obfuscated})'
HTTP POST /Integration responded 400 in 37.0021 ms
Request finished HTTP/1.1 POST http://{obfuscated}/Integration - 400 null application/problem+json; charset=utf-8 37.7163ms
Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.ProblemDetails'.
Executed action {obfuscated}.IntegrationController.MatchIdentityAsync ({obfuscated}) in 26.5296ms
Executed endpoint '{obfuscated}.Controllers.IntegrationController.MatchIdentityAsync ({obfuscated})'
HTTP POST /Integration responded 400 in 37.0021 ms
Request finished HTTP/1.1 POST http://{obfuscated}/Integration - 400 null application/problem+json; charset=utf-8 37.7163ms
This is how my controller looks:
[ApiController]
[Route("[controller]")]
public class IntegrationController : ControllerBase
{
// ...

[HttpPost]
// [Authorize]
public async Task<IActionResult> MatchIdentityAsync([FromBody] IntegrationAuthDTO reqDTO)
{
_logger.LogInformation($"Matching identity, Nonce - {reqDTO.Nonce}, Integration - {reqDTO.IntegrationType}");
}
}

// IntegrationAuthDTO
public class IntegrationAuthDTO
{
public string Nonce { get; set; } = default!;
public string IntegrationType { get; set; } = default!;
}
[ApiController]
[Route("[controller]")]
public class IntegrationController : ControllerBase
{
// ...

[HttpPost]
// [Authorize]
public async Task<IActionResult> MatchIdentityAsync([FromBody] IntegrationAuthDTO reqDTO)
{
_logger.LogInformation($"Matching identity, Nonce - {reqDTO.Nonce}, Integration - {reqDTO.IntegrationType}");
}
}

// IntegrationAuthDTO
public class IntegrationAuthDTO
{
public string Nonce { get; set; } = default!;
public string IntegrationType { get; set; } = default!;
}
Any ideas why this is happening?
9 Replies
Natro
Natro3mo ago
Tried setting following as well:
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
tera
tera3mo ago
what's the actual response you get?
Natro
Natro3mo ago
For localhost my business logic goes through and I return 204. For production it seems I don't even enter the endpoint and it is being stopped by the MVC 400 handling. Does that answer your question?
tera
tera3mo ago
but what is the content of the response when you get 400 it should contain something useful
Natro
Natro3mo ago
When called from next.js app I get this in network response browser tab:
No description
Natro
Natro3mo ago
My request matches my DTO:
No description
tera
tera3mo ago
and you request is exactly the same? in both scenario i would enable more verbose logging in prod might provide more clues
Natro
Natro3mo ago
How can I enable more verbose logging for aspnet / mvc validation? The thing is, I have Serilog enabled. In my endpoint I log stuff, which doesn't get logged (stuff in Program.cs gets logged) so I assume, it doesnt even enter my endpoint. In my Program.cs I do following:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger();

try
{
// This I can see in the console
Log.Information("Starting web application");

var builder = WebApplication.CreateBuilder(args);

// ...

builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console());

// ...
}
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger();

try
{
// This I can see in the console
Log.Information("Starting web application");

var builder = WebApplication.CreateBuilder(args);

// ...

builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console());

// ...
}
Then in the endpoint it doesnt seem anything is logged. I have suspicion I actually enter the endpoint but inside I also return 400 after some other validation
tera
tera3mo ago
.MinimumLevel.Debug() should do it but asp net core middlewares log other stuff on their own, which should help
Want results from more Discord servers?
Add your server
More Posts