-
First Check
Commit to Help
Example Codefrom typing import Annotated
import arrow
from fastapi import FastAPI, Response
app = FastAPI()
def set_no_cache_headers(response: Response) -> Response:
response.headers["Expires"] = arrow.utcnow().format(arrow.FORMAT_RFC1123)
response.headers["Cache-Control"] = "max-age=0, no-cache, no-store, must-revalidate, private"
return response
class NoCacheResponse:
def __init__(self, header_directives: List[str]) -> None:
self.header_directives = header_directives
def __call__(self, response: Response) -> None:
if "no_cache" in self.header_directives:
set_no_cache_headers(response=response)
NO_CACHE_RESPONSE = NoCacheResponse(["no_cache"])
@app.get("/testing/")
def testing_response(
response: Annotated[Response, Depends(NO_CACHE_RESPONSE)],
):
return {} DescriptionPreviously (up to FastAPI 0.89.1, which is the last version I used before trying to update), the code above worked just fine. I use this to add caching headers to my GET responses (as certain network devices will cache unless specifically told not to). I built this as the docs here specify that Now, FastAPI specifically block this functionality. Lines 383-387 of
checks to see if the parameter is of certain types, and immediately fails indicating My question is this: Which is correct, the code or the docs? If I'm simply misunderstanding how to Operating SystemLinux Operating System DetailsNo response FastAPI Version0.100.0 Python Version3.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Can you reproduce with fastapi 0.99.0? |
Beta Was this translation helpful? Give feedback.
-
This is a bug. For the time being, you can do the following as a workaround: from typing import Annotated
from fastapi import FastAPI, Response, Depends
app = FastAPI()
def set_no_cache_headers(response: Response) -> Response:
response.headers["Potato"] = "potato"
return response
@app.get("/")
def testing_response(response = Depends(set_no_cache_headers)):
return {} |
Beta Was this translation helpful? Give feedback.
This is a bug.
For the time being, you can do the following as a workaround: