After upgrading FastAPI to 0.143, an internal client's JSON POSTs sent without a Content-Type header now fail with 422; why did FastAPI add this check, and how do you fix it?
answer
- a breaking change in 0.132
- no header, no JSON parsing
- requests that skip the preflight
- unauthenticated local apps
- router-level opt-out
basics
~20 sSince FastAPI 0.132, strict_content_type defaults to True, so a body without a Content-Type header is not parsed as JSON and fails validation with 422. It blocks header-less cross-site browser requests that skip the CORS preflight; make the client send application/json.
solid answer
~40 sFastAPI 0.132 made `strict_content_type=True` the default, and 0.143 keeps it. A body with no `Content-Type` header is no longer parsed as JSON; the raw bytes fail model validation, so the client sees 422. A body labelled `application/json` or `application/...+json` parses as before, and a `text/plain` body has not been parsed as JSON since 0.65.2. The motive is a cross-site request: a page can `fetch()` a `Blob` body with no `Content-Type` and no credentials, the browser sends it without a CORS preflight, and an unauthenticated app on `localhost` or an internal network would have executed it. Fix the client to send `application/json`. If you cannot, put those routes on an `APIRouter(strict_content_type=False)` instead of relaxing the whole app.
code
python · 34 linesfrom fastapi import APIRouter, FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
class Order(BaseModel):
sku: str
quantity: int = 1
app = FastAPI() # strict_content_type defaults to True
legacy = APIRouter(prefix="/legacy", strict_content_type=False)
@app.post("/orders")
async def create_order(order: Order):
return order
@legacy.post("/orders")
async def create_legacy_order(order: Order):
return order
app.include_router(legacy)
client = TestClient(app)
raw = b'{"sku": "LAMP-01"}'
json_header = {"Content-Type": "application/json"}
print(client.post("/orders", content=raw).status_code) # 422
print(client.post("/orders", content=raw, headers=json_header).status_code) # 200
print(client.post("/legacy/orders", content=raw).status_code) # 200
text_header = {"Content-Type": "text/plain"}
print(client.post("/legacy/orders", content=raw, headers=text_header).status_code) # 422go deeper
Remember that a FastAPI JSON body needs a JSON Content-Type such as application/json; since 0.132 a request without the header fails with 422.
Explain how FastAPI decides a body is JSON: no header, a json or +json subtype, or anything else, and what strict_content_type changes in that path.
Diagnose the post-upgrade 422 from a header-less client, explain the preflight-skipping request the default blocks, and scope any opt-out to one APIRouter instead of the app.
Decide when a team may disable the check: only for clients it controls and cannot fix, never for unauthenticated apps reachable from a browser, and record the exception.
## What changed in 0.132 FastAPI 0.132.0 added **`strict_content_type`**, listed under breaking changes, and turned it on by default. FastAPI 0.143 keeps that default. Before 0.132, when a request carried a body but **no `Content-Type` header at all**, FastAPI parsed the body as JSON anyway. Since 0.132 it does not: the body stays raw bytes, the Pydantic body parameter cannot be built from them, and the request is answered with a **422** validation error. The JSON in the request can be perfectly valid; it is never parsed. That is why an internal client that never bothered to send a header breaks after an upgrade, while browsers and HTTP client libraries that set `application/json` keep working. ## How FastAPI decides a body is JSON In FastAPI 0.143 the request handler for a JSON body parameter follows these steps: 1. Read the body bytes. An empty body skips parsing; a required body is then reported missing. 2. If there is **no `Content-Type` header**, parse as JSON only when `strict_content_type` is `False`. 3. If there **is** a header, parse as JSON only when its main type is `application` and its subtype is `json` or ends in `+json`. Parameters such as `; charset=utf-8` do not matter, so `application/json; charset=utf-8` and `application/vnd.api+json` both parse. 4. Otherwise pass the raw bytes on to validation, which rejects them for a model parameter. Step 3 is not affected by the setting, and it did not change in 0.132: a body labelled `text/plain` or `application/x-www-form-urlencoded` has not been parsed as JSON since FastAPI 0.65.2. Only the **missing header** case changed. ## Why: the cross-site request that skips the preflight Browsers protect cross-origin requests with **CORS**. A cross-origin `fetch()` that sends `Content-Type: application/json` triggers a **preflight** `OPTIONS` request, and the server can refuse it before the real request is sent. Some requests are "simple" and are sent without a preflight. FastAPI's documentation describes the gap it closed: a script can send a `fetch()` with a `Blob` body that has **no `Content-Type`**, without credentials, and the browser sends it straight away. CORS only stops the attacker's page from reading the response; the request itself still reaches the server and runs. Under the old behaviour FastAPI parsed that header-less body as JSON, so a malicious web page could drive a JSON endpoint. The labelled route had been closed years earlier: FastAPI 0.65.2 (2021) stopped parsing `text/plain` bodies as JSON after a CSRF advisory, because `text/plain` is also exempt from preflights. The missing header was the remaining path, and 0.132 closed it. ## Who was exposed FastAPI's documentation is specific about the scenario: - **Exposed:** an app running on `localhost` or an internal network with **no authentication**, trusting that only local users can reach it. Its example is a local AI agent server with a browser frontend; any web page the user visits could have sent it commands. - **Not the target:** an app on the open internet that already authenticates privileged endpoints. Attackers can script requests directly, without a browser, so authentication is what protects it. That makes `strict_content_type` a narrow defence that closes one browser path, aimed mainly at unauthenticated local and internal apps. It does not replace authentication, or a CSRF strategy for cookie-based sessions. ## Fixing the client or relaxing the check | Option | Scope | Effect | |---|---|---| | Client sends `Content-Type: application/json` | that client | The right fix; nothing on the server changes | | `APIRouter(strict_content_type=False)` | routes on that router | Header-less bodies parse as JSON there; the rest of the app stays strict | | `FastAPI(strict_content_type=False)` | the whole app | Restores the pre-0.132 behaviour everywhere | Things that do **not** work: - `Body(media_type=...)` changes the media type shown in the generated OpenAPI document; FastAPI's docstring says it does not affect parsing. - The path operation decorators such as `@app.post` do not take a `strict_content_type` argument; the setting lives on `FastAPI()`, on `APIRouter()` and on `APIRouter.add_api_route`. - `strict_content_type=False` never makes a `text/plain` body parse as JSON. Relax the check only for clients you control and cannot fix, and never on an unauthenticated app reachable from a browser, which is exactly the app the default protects. ## Diagnosing it The symptom is a 422 for a body that looks correct in the client's logs. Check three things: 1. Does the failing request carry a `Content-Type` header at all? Proxies and hand-rolled clients sometimes drop it. 2. Is the header a JSON type (`application/json` or `application/...+json`)? 3. Was the service upgraded across 0.132? The release notes list the change as breaking.
- Does `strict_content_type=False` make FastAPI accept a JSON body sent with `Content-Type: text/plain`?No. The setting only covers requests with no `Content-Type` header at all. A body with a non-JSON type such as `text/plain` is not parsed as JSON whatever the setting, so it still fails validation with 422. The client has to send `application/json` or another `+json` type.
- In FastAPI 0.143, how do you relax the Content-Type check for one legacy integration without relaxing it for the whole app?Declare the integration's routes on `APIRouter(strict_content_type=False)` and include that router. The router's setting applies to its routes, while the app keeps the strict default for everything else. Decorators such as `@router.post` do not accept the argument; only `APIRouter.add_api_route` takes it per route, so a dedicated router is the usual scope.
- Does the FastAPI 0.132 Content-Type check protect a public API whose privileged endpoints already require a bearer token?Not meaningfully, and FastAPI's documentation says the risk does not apply there: an attacker can send requests directly with a script, so authentication is the protection. The check targets apps on `localhost` or an internal network that rely on the network as their only protection.
- FastAPI answers 415 Unsupported Media Type when the Content-Type header is missing
- strict_content_type=False makes FastAPI parse text/plain bodies as JSON
- Body(media_type=...) lets an endpoint accept JSON sent without a header
- Passing strict_content_type=False to @app.post relaxes a single route
- Before 0.132 FastAPI parsed any body as JSON whatever its Content-Type
Assumes FastAPI 0.143; strict_content_type arrived in 0.132 defaulting to True, and before that a body without a Content-Type header was parsed as JSON.