Http headers can be modified in fastAPI; however when I tried to use faster serializer (in this instance ORJSONResponse) in order to have faster response, all added or modified headers confusingly is being vanished. I don't have any idea that why it happens and how can I fix it?
also we got the same result for UJSONResponse too.
thanks for sharing your experiences.
from fastapi import APIRouter, Response
from fastapi.responses import ORJSONResponse
router = APIRouter()
@router.get("/test")
async def plan_list(response:Response):
response.headers["test-header"]="test-value"
return ORJSONResponse(list(range(10000)))
date: Thu, 22 Oct 2020 18:42:19 GMT
server: uvicorn
content-length: 48891
content-type: application/json
from fastapi import APIRouter, Response
from fastapi.responses import ORJSONResponse
router = APIRouter()
@router.get("/test")
async def plan_list(response:Response):
response.headers["test-header"]="test-value"
return list(range(10000))
date: Thu, 22 Oct 2020 18:42:19 GMT
server: uvicorn
content-length: 48891
content-type: application/json
test-header: test-value
test platform:
try this
from fastapi import APIRouter, Response
from fastapi.responses import ORJSONResponse
router = APIRouter()
@router.get("/test", response_class=ORJSONResponse)
async def plan_list(response: Response):
response.headers["test-header"] = "test-value"
return list(range(10000))
@includeamin Actually, as I tested before, using response_class=ORJSONResponse is more than 10 times slower than calling the ORJSONResponse()directly. Specifically when data is very large object >10 MB I have to use it as function call.
I'm not sure that it's good to do that or not.
if you want to directly use that:
from fastapi import APIRouter, Response, FastAPI
from fastapi.responses import ORJSONResponse
app = FastAPI()
router = APIRouter()
@router.get("/test")
async def plan_list():
response = ORJSONResponse(list(range(10000)))
response.headers.update({"test-header": "test-value"})
return response
app.include_router(router)
@includeamin Yes thanks, it seems good idea. With inspiration of your answer I found another way. what about this one?
from fastapi import APIRouter, Response, FastAPI
from fastapi.responses import ORJSONResponse
app = FastAPI()
router = APIRouter()
@router.get("/test")
async def plan_list(response:Response):
response.headers["test"]="test-value"
return ORJSONResponse(list(range(10000)),headers=response.headers)
app.include_router(router)
@includeamin Yes thanks, it seems good idea. With inspiration of your answer I found another way. what about this one?
from fastapi import APIRouter, Response, FastAPI from fastapi.responses import ORJSONResponse app = FastAPI() router = APIRouter() @router.get("/test") async def plan_list(response:Response): response.headers["test"]="test-value" return ORJSONResponse(list(range(10000)),headers=response.headers) app.include_router(router)
sounds good!
@includeamin thanks once more . I close this issue
Most helpful comment
@includeamin thanks once more . I close this issue