Documentation
Docs
Introduction
Getting Started
Tutorial: build an app
Core Concepts
Routing
Server-Side Rendering
Mimir, state management
Pages & Layouts
API Routes
Styling & Theming
Building & Deploying
API Reference
Guides
Recipes
FAQ (use cases)
validateContentType()
Reject requests whose body is not a content type you accept, with 415 Unsupported Media Type.
Use case
A handler that expects JSON should not have to defend against a form-encoded or text/html body. Enforcing
the content type up front defends against type-confusion bugs and sloppy clients, and keeps your parsing
code honest: by the time the handler runs, the body is a type it knows.
How it works
It applies to body methods (POST, PUT, PATCH) and compares the request's Content-Type (ignoring
parameters like ; charset=utf-8) against the allowed types list, by exact match. A mismatch, or a missing
content type when allowEmpty is off, replies 415 Unsupported Media Type. GET/HEAD/DELETE pass
through untouched.
Configuration
| Option | Default | Notes |
|---|---|---|
types | application/json, text/plain, application/x-www-form-urlencoded, multipart/form-data | Allowed media types (exact match, params ignored). |
allowEmpty | false | Allow a body method with no content type. Off by default, an empty type is rejected. |
Example
Notes
Narrow
typesto exactly what your API accepts. A JSON API that only ever listsapplication/jsonwill reject a straymultipart/form-dataprobe with a clean415instead of failing deeper in a parser.
Next: csrf.