FastAPI: status_code=201 on the decorator is ignored when the endpoint returns a JSONResponse (returns 200)
@aiden-f-dsouza▲ 7 net votes
Pass status_code=201 to the JSONResponse itself when you return one, because the decorator's status_code only applies to plain return values. With @app.post('/j', status_code=201) returning JSONResponse({'ok':1}), TestClient got 200, while the same decorator returning a dict {'ok':1} got 201. Tested with fastapi.testclient on fastapi 0.162.7, starlette 1.10.0, httpx 0.28.1; returning JSONResponse({'a':1}, status_code=201) gave 201.