Node.js, PHP and APIs · Troubleshooting walkthrough
Web API answers 400 with One or more validation errors occurred
Short answer
One or more validation errors occurred is the title of the automatic 400 an [ApiController] returns when binding or validation fails; its errors object names the field. Triggers: a missing non-nullable string with Nullable enabled, a JSON value of the wrong type, or an empty body. Fix the payload or mark optional properties nullable. Applies to .NET 10 and 11.
One or more validation errors occurred is not an exception message. It is the title of the automatic 400 response that an [ApiController] returns when model binding or validation fails, before your action method runs. The useful part is the errors object next to it, which names the field and the reason. Read that, and you know which of three triggers you have.
The three are a missing property under nullable reference types, a JSON value of the wrong type, and an empty body.
The text you see
"title": "One or more validation errors occurred."The Name field is required.The JSON value could not be converted to System.Int32.A non-empty request body is required.
Stage 1
Send the failing request and read the errors object
Reproduce it outside the app
curl -s -X POST https://localhost:5001/api/people -H "Content-Type: application/json" -d '{"age": 30}'- A property name with
The Name field is required.: the first trigger. - A key starting with
$.and a message about converting a JSON value: the second. - An empty key holding
A non-empty request body is required.: the third.
Stage 2
A non-nullable string you did not mark as required
ASP.NET Core treats non-nullable reference properties as if they carried [Required(AllowEmptyStrings = true)]. The model validation documentation says that in a project with Nullable enabled, a missing value for such a property in a JSON or form post produces a validation error, even if the class has no [Required] attribute.
Mark properties the client may omit as nullable, or make the client send them. Switching the implicit rule off for the whole app is possible, but it hides real mistakes, so prefer nullability per property.
Optional means nullable
public class Person
{
public string Name { get; set; } = ""; // still required
public string? Nickname { get; set; } // now optional
public int Age { get; set; }
}
// app-wide alternative (Program.cs):
// builder.Services.AddControllers(options =>
// options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);Stage 3
A JSON value that cannot become the C# type
A JsonException from System.Text.Json, such as "30" in quotes for an int or a text value for an enum, is caught by the JSON input formatter and added to ModelState under the JSON path. The [ApiController] filter then returns the standard validation response. Send the type the model expects, or register the string enum converter when clients send enum names.
Accept enum names as strings
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});Stage 4
An empty body reaches a FromBody parameter
An empty body sent to a [FromBody] parameter fails with the same title. A typical cause is a fetch call that passes a plain object where JSON.stringify(data) was needed. Only if the endpoint really accepts no body, allow that on the one parameter.
Serialise on the client, allow empty on the server only if valid
// client
await fetch("/api/people", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Ana", age: 30 })
});
// server, only when an empty body is valid
public IActionResult Post([FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] Person? person)Stage 5
Make the next 400 easy to read in the logs
- Log automatic 400 responses through
InvalidModelStateResponseFactory, so the failing field appears in server logs and not only in the client. - When a project switches Nullable on, review every request DTO and mark each property the client may omit with
?. - In your own actions, return
ValidationProblem()rather thanBadRequest(), as the web API documentation advises, so clients parse oneValidationProblemDetailsshape.
If you only keep one thing
- The title belongs to the automatic 400; the
errorsobject names the field. - Nullable reference types make missing strings required.
- Check the JSON type and the body before blaming the server.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.