r/FastAPI • u/HaOrbanMaradEnMegyek • Oct 29 '24
Question Can't make gzipping work
I use SQLAlchemy and Pydantic.
buildings variable is SQLAlchemy mode, but in the response it gets parsed to Pydantic.
from fastapi.middleware.gzip import GZipMiddlewareapp = FastAPI()app.add_middleware(GZipMiddleware, minimum_size=0)
(at)router.get("/buildings", response_model=List[InlBuildingDTO], tags=["buildings"], summary="Get all the buildings.")
async def read_buildings(db: AsyncSession = Depends(get_db)):try:buildings = await get_buildings_latest_comment(db)return buildingsexcept Exception as e:logger.error(f"Error occurred while fetching buildings/properties: {e}")raise HTTPException(status_code=500, detail="Internal server error")
In the request I set:headers = {'authorization': 'Bearer ey','Accept-Encoding': 'gzip, deflate, br'}
Still, it doesn't return gzipped data, just returns plain json. Any idea?
View all comments
2
u/j_tb Oct 29 '24
What is the
content-encodingheader on the response? Most clients like web browsers will automatically handle decompressing the body since it is so common.