Skip to content

direct return of Element does not work in FastAPI route handler with default_response_class set #54

@blast25

Description

@blast25

https://htpy.dev/starlette/

Usage with FastAPI:

app=FastAPI()

test_route= APIRouter(
    prefix="/test",
    tags=["test"],
    default_response_class=HTMLResponse,
)

@test_route.get("/")
async def test():
    return div["test"]


app.include_router(test_route)

if __name__ == "__main__":
    uvicorn.run("test:app", host="0.0.0.0", port=9001, reload=True)

Calling localhost:9001/test/ returns error

Does not work for me as the internal FastApi Encoding does not know what to do with it (expects pydantic model). Using HTMLResponse as return works. But using a response from default response class fails for me, this would be necassary as to not delete changes in middleware or dependencies for routes that manipulate eg. the response header. Initializing HTMLReponse yourself would entail setting everything again from previous functions, every time.
Workaround is using return str(div["test"]).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions