How does content negotiation handle the request body (input formatters) differently from the response body (output formatters)?
Short answer
Input formatters (like the built-in JSON input formatter) deserialize the incoming request body based on its Content-Type header (not Accept, which governs the response), converting raw bytes into the strongly-typed parameter your action method expects -- a mismatch between the…
Advertisement
AModel answer
Input formatters (like the built-in JSON input formatter) deserialize the incoming request body based on its Content-Type header (not Accept, which governs the response), converting raw bytes into the strongly-typed parameter your action method expects -- a mismatch between the actual Content-Type header and the request body's real format (or an unsupported Content-Type with no matching input formatter) results in a 415 Unsupported Media Type response before your action code even runs.
// Client sends Content-Type: application/json with a JSON body -- input formatter deserializes it
[HttpPost]
public IActionResult CreateProduct([FromBody] ProductDto dto) { ... }
// Client sends Content-Type: application/xml but no XML input formatter registered:
// -> 415 Unsupported Media Type, action never executesReal-world example
A client accidentally sending Content-Type: text/plain with a JSON-formatted body receives a 415 Unsupported Media Type response rather than a confusing model binding failure, since no input formatter registered for text/plain can even attempt to deserialize the request.
Likely follow-ups
- What's the difference between 415 (wrong Content-Type) and 400 (malformed body of the correct type)?
- How would you add a custom input formatter for a non-standard request format?
Related topics