I would like to post data to my API using AJAX but I'm having issues. I'm using Fiddler to test my API and I'm able to post JSON correctly but when posting a name/value urlencoded string I get a 400 Bad Request with the response body being '{"":["The input was not valid."]}'.
My debug window displays: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor:Information: Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.SerializableError'.
The JSON being posted is:
{
"Name": "Test"
}
The form data being posted is:
Name=Test
This is the Controller and Action:
[Route("api/[Controller]")]
[ApiController]
public class AccountsController : Controller
{
[HttpPost]
public IActionResult CreateAccount(Account account)
{
//code
}
}
This is the Account class:
public class Account
{
public string Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Website { get; set; }
}
It seems obvious that there is an issue during model binding but the form data seems valid (I've also generated form data using AJAX and get a 400 as well).
See Question&Answers more detail:os