starlette

How to get the cookies from an HTTP request using FastAPI?

How to get the cookies from an HTTP request using FastAPI? Question: Is it possible to get the cookies when someone hits the API? I need to read the cookies for each request. @app.get("/") async def root(text: str, sessionKey: str = Header(None)): print(sessionKey) return {"message": text+" returned"} if __name__ == "__main__": uvicorn.run("main:app", host="0.0.0.0", port=5001 ,reload=True) …

Total answers: 2

How to load a different file than index.html in FastAPI root path?

How to load a different file than index.html in FastAPI root path? Question: Here is a simple static FastAPI app. With this setup even though the root path is expected to return a FileResponse of custom.html, the app still returns index.html. How can I get the root path work and render custom.html? from fastapi import …

Total answers: 1

How to pass a URL as a path parameter to a FastAPI route?

How to pass URL as a path parameter to a FastAPI route? Question: I have created a simple API using FastAPI, and I am trying to pass a URL to a FastAPI route as an arbitrary path parameter. from fastapi import FastAPI app = FastAPI() @app.post("/{path}") def pred_image(path:str): print("path",path) return {‘path’:path} When I test it, …

Total answers: 2

How to return a custom 404 Not Found page using FastAPI?

How to return a custom 404 Not Found page using FastAPI? Question: I am making a rick roll site for Discord and I would like to redirect to the rick roll page on 404 response status codes. I’ve tried the following, but didn’t work: @app.exception_handler(fastapi.HTTPException) async def http_exception_handler(request, exc): … Asked By: AshKetchumPL || Source …

Total answers: 2

FastAPI – How to get app instance inside a router?

FastAPI – How to get app instance inside a router? Question: I want to get the app instance in my router file, what should I do ? My main.py is as follows: # … app = FastAPI() app.machine_learning_model = joblib.load(some_path) app.include_router(some_router) # … Now I want to use app.machine_learning_model in some_router’s file , what should …

Total answers: 1

Send query params from Jinja template

Send query params from Jinja template Question: I can’t figure out if it’s possible to pass query parameters from a static html page to a view for processing. I have now implemented the functionality I need using path parameters. I want to do the same, but with query parameters main.py from helpers.utils import CustomURLProcessor app …

Total answers: 1

fastapi (starlette) RedirectResponse redirect to post instead get method

fastapi (starlette) RedirectResponse redirect to post instead get method Question: I have encountered strange redirect behaviour after returning a RedirectResponse object events.py router = APIRouter() @router.post(‘/create’, response_model=EventBase) async def event_create( request: Request, user_id: str = Depends(get_current_user), service: EventsService = Depends(), form: EventForm = Depends(EventForm.as_form) ): event = await service.post( … ) redirect_url = request.url_for(‘get_event’, **{‘pk’: …

Total answers: 2

FastAPI not loading static files

FastAPI is not loading static files Question: So, I’m swapping my project from node.js to python FastAPI. Everything has been working fine with node, but here it says that my static files are not present, so here’s the code: from fastapi import FastAPI, Request, WebSocket from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from fastapi.templating …

Total answers: 3

How to delete the file after a `return FileResponse(file_path)`

How to delete the file after a `return FileResponse(file_path)` Question: I’m using FastAPI to receive an image, process it and then return the image as a FileResponse. But the returned file is a temporary one that need to be deleted after the endpoint return it. @app.post("/send") async def send(imagem_base64: str = Form(…)): # Convert to …

Total answers: 4