liveBook · Manning

400 bad request

As explained in ASP.NET Core in Action, 3e

A 400 status code is an HTTP response status indicating that the server could not process the request because of invalid client input.

A 400 Bad Request response tells an HTTP client that its request was invalid, commonly because submitted data failed validation or could not be parsed. section 5.3.1 section 5.6

Minimal APIs can produce this response when:

  • A client sends an incompatible value, such as the string "two" for an integer parameter. section 7.2
  • A required parameter cannot bind from the selected route, query-string, header, or body source. section 7.8
  • An endpoint deliberately rejects invalid application data with Results.BadRequest or a typed result. section 5.3.1

Rejecting a duplicate resource

A POST endpoint attempts to add a fruit. If the identifier already exists, it returns Results.BadRequest with an explanatory error; otherwise, it returns 201 Created. section 5.3.1

Figure 7.3 The routing middleware matches the incoming URL to the endpoint. The endpoint middleware attempts to bind the route parameter id to the endpoint parameter. The endpoint parameter type ProductId implements TryParse. If parsing is successful, the parsed parameter is used to call the endpoint handler. If parsing fails, the endpoint middleware returns a 400 Bad Request response.
CH07_F03_Lock3

Follow the binding flow from route matching through TryParse; unsuccessful parsing ends in a 400 response before the handler runs. section 7.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains explicit results and automatic binding failures, but stops short of defining a complete validation-error format.
Examples
Uses duplicate fruit identifiers, incompatible integer input, and failed ProductId parsing to demonstrate 400 responses.
What it assumes
Assumes basic HTTP requests and minimal API endpoints while teaching status-code meaning and binding failures through examples.
What you can do after
Diagnose invalid route, query, header, or body input and return a deliberate 400 response from a minimal API endpoint.
How it connects
Connects HTTP APIs with minimal APIs, model binding, validation, Results, TypedResults, and TryParse.

Also explained in

Compare how 2 books explain 400 bad request →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.