Repository navigation
Plain text validation error tutorial in docs not producing plain text #13278
Answered
by
luzzodev
callumforrester
asked this question in
Questions
First Check
Commit to Help
Example Codeimport uvicorn
from fastapi import FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from fastapi.responses import PlainTextResponse
from starlette.exceptions import HTTPException as StarletteHTTPException
app = FastAPI()
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request, exc):
return PlainTextResponse(str(exc.detail), status_code=exc.status_code)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
return PlainTextResponse(str(exc), status_code=400)
@app.get("/items/{item_id}")
async def read_item(item_id: int):
if item_id == 3:
raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
return {"item_id": item_id}
uvicorn.run(app, host="0.0.0.0", port=8000)DescriptionFollowing the tutorial in the docs page about overriding request validation exceptions does not seem to produce the expected result. I run the code provided (see my example) but do not get the output the docs suggest: The last command should produce a plain text response. So far Just posting to see if others can reproduce this. If so, do the docs need updating? Operating SystemLinux Operating System DetailsUsing container: https://hub.docker.com/_/python (3.11), host machine is Fedora 41 FastAPI Version0.115.7 Pydantic Version2.10.6 Python Version3.11.9 Additional ContextNo response |
Answered by
luzzodev
Jan 31, 2025
Replies: 1 comment 8 replies
|
The problem is that the endpoint is expecting an int for |
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@callumforrester based on the docs example, the closest and simple thing you can do is something like that