How to read camelCase JSON request from http body?

199 Views Asked by At

I have following API endpoint

public class CreateSupplierCommand
{
    public string Name { get; set; } = null!;
    public string? Website { get; set; }
}
    
[ApiController]
public class SuppliersController : ControllerBase
{
    // ...

    [HttpPost, Route("api/supplier")]
    public IActionResult Post([FromBody] CreateSupplierCommand command)
    {
        var supplier = new Supplier(command.Name, command.Website);
        _context.Suppliers.Add(supplier);
        _context.SaveChanges();
        return Ok();
    }
}

and the following request payload with PascalCase works. The endpoint is called and supplier is created.

{Name: "foo", Website: "bar"}

but camelCase attributes does not. The endpoint is not even called

{name: "foo", website: "bar"}

I am not sure, but I think in .NET6 it worked. Any idea?

1

There are 1 best solutions below

0
On BEST ANSWER

It seems that following code in Program.cs fix the issue

builder.Services.AddControllersWithViews().AddJsonOptions(options =>
{
        options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});

I found answer here