Questions tagged [starlette]

Questions about Starlette (a lightweight python ASGI framework/toolkit).

Starlette is a lightweight ASGI framework/toolkit, which is ideal for building high performance asyncio services.

https://www.starlette.io

259 questions
46
votes
2 answers

How to Upload File using FastAPI?

I am using FastAPI to upload a file according to the official documentation, as shown below: @app.post("/create_file") async def create_file(file: UploadFile = File(...)): file2store = await file.read() # some code to store the…
Aric
  • 461
  • 1
  • 4
  • 3
43
votes
11 answers

FastAPI (starlette) get client real IP

I have an API on FastAPI and i need to get the client real IP address when he request my page. I'm ty to use starlette Request. But it returns my server IP, not client remote IP. My code: @app.post('/my-endpoint') async def my_endpoint(stats: Stats,…
Nataly Firstova
  • 661
  • 1
  • 5
  • 12
27
votes
3 answers

FastAPI, return a File response with the output of a sql query

I'm using FastAPI and currently I return a csv which I read from SQL server with pandas. (pd.read_sql()) However the csv is quite big for the browser and I want to return it with a File…
SDuma
  • 303
  • 1
  • 3
  • 8
23
votes
2 answers

RuntimeError: No response returned in FastAPI when refresh request

I got this error in my application and i didn't know why. After many search and debugging just figured out that it happens when i refresh my request before getting response(cancel request and send another request while processing previous request).…
Alichszn
  • 231
  • 1
  • 2
  • 3
22
votes
5 answers

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

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 =…
Kleyson Rios
  • 2,597
  • 5
  • 40
  • 65
19
votes
2 answers

Graceful shutdown of uvicorn starlette app with websockets

Given this sample Starlette app with an open websocket connection, how do you shut down the Starlette app? I am running on uvicorn. Whenever I press Ctrl+C the output is Waiting for background tasks to complete. which hangs forever. from…
Neil
  • 8,925
  • 10
  • 44
  • 49
18
votes
2 answers

Using FastAPI in a sync way, how can I get the raw body of a POST request?

Using FastAPI in a sync, not async mode, I would like to be able to receive the raw, unchanged body of a POST request. All examples I can find show async code, when I try it in a normal sync way, the request.body() shows up as a coroutine…
576i
  • 7,579
  • 12
  • 55
  • 92
18
votes
2 answers

How to test a FastAPI api endpoint that consumes images?

I am using pytest to test a FastAPI endpoint that gets in input an image in binary format as in @app.post("/analyse") async def analyse(file: bytes = File(...)): image = Image.open(io.BytesIO(file)).convert("RGB") stats =…
Davide Fiocco
  • 5,350
  • 5
  • 35
  • 72
15
votes
2 answers

How to call an api from another api in fastapi?

I was able to get the response of one API from another but unable to store it somewhere(in a file or something before returning the response) response=RedirectResponse(url="/apiname/") (I want to access a post request with header and body) I want…
Sudip Kandel
  • 459
  • 1
  • 4
  • 9
14
votes
1 answer

FastAPI how to add ZMQ to eventloop

I am surprised this has not really been asked in detail but for some reason i could not find this question or solution anywhere. It seems to be that a lot of people are having a problem where you have a fastAPI application that also needs to…
user3554230
  • 283
  • 2
  • 11
14
votes
2 answers

Raise exception in python-fastApi middleware

I am trying to validate token in fastapi middleware but this seems impossible. As i am thinking middleware needs to make next call although its not required. I am not able to find any good solution to handle token in one go in this python-fastapi…
TrickOrTreat
  • 821
  • 1
  • 9
  • 23
14
votes
4 answers

FastAPI middleware peeking into responses

I try to write a simple middleware for FastAPI peeking into response bodies. In this example I just log the body content: app = FastAPI() @app.middleware("http") async def log_request(request, call_next): logger.info(f'{request.method}…
FireAphis
  • 6,650
  • 8
  • 42
  • 63
13
votes
1 answer

FastAPI - How to use HTTPException in responses?

The documentation suggests raising an HTTPException with client errors, which is great. But how can I show those specific errors in the documentation following HTTPException's model? Meaning a dict with the "detail" key. The following does not work…
Mojimi
  • 2,561
  • 9
  • 52
  • 116
13
votes
2 answers

FastAPI/Pydantic accept arbitrary post request body?

I want to create a FastAPI endpoint that just accepts an arbitrary post request body and returns it. If I send {"foo" : "bar"} , I want to get {"foo" : "bar"} back. But I also want to be able to send {"foo1" : "bar1", "foo2" : "bar2"} and get that…
Jabrove
  • 718
  • 5
  • 13
11
votes
2 answers

FastAPI dependencies (yield): how to call them manually?

FastAPI uses Depends() to inject variables either returned or yielded. Eg, FastAPI/SQL: # Dependency def get_db(): db = SessionLocal() try: yield db finally: db.close() ... def create_user(db: Session =…
lefnire
  • 2,524
  • 2
  • 26
  • 36
1
2 3
17 18