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
5
votes
2 answers

relative URL paths when serving static files with FastAPI/Starlette

I have a simple FastAPI application that serves a file test.html in app/main.py like so: @app.get('/') def index(): return FileResponse('static/test.html') The directory structure is like so: app/main.py app/static/test.html Can I change this do…
rookie099
  • 2,201
  • 2
  • 26
  • 52
4
votes
2 answers

How to download a large file using FastAPI?

I am trying to download a large file (.tar.gz) from FastAPI backend. On server side, I simply validate the filepath, and I then use Starlette.FileResponse to return the whole file—just like what I've seen in many related questions on…
Yuqi Wang
  • 135
  • 1
  • 2
  • 8
4
votes
1 answer

Why do UVICORN/Starlette/FastAPI spawn more threads when not using "ASYNC" and don't when using "ASYNC"?

Okay, so I was working on a comparison study on using ASYNC vs without using ASYNC in FastAPI. However, I have arrived at some unexpected results, and don't understand why. Here is the setup 1: THE API WHICH DOES NOT USE ASYNC import uvicorn from…
raghavsikaria
  • 867
  • 17
  • 30
4
votes
5 answers

FastAPI: How to get raw URL path from request?

I have a GET method with requested parameter in path: @router.get('/users/{user_id}') async def get_user_from_string(user_id: str): return User(user_id) Is it possible to get base url raw path (i.e., '/users/{user_id}') from the request? I have…
Kirill Marks
  • 75
  • 2
  • 7
4
votes
1 answer

FastAPI: Some requests are failing due to 10s timeout

We have deployed a model prediction service in production that is using FastAPI and unfortunately, some of the requests are failing due to a 10s timeout. In terms of concurrent requests, we typically only load about 2/3 requests per second, so I…
Riley Hun
  • 2,541
  • 5
  • 31
  • 77
4
votes
1 answer

Why does fastapi hang when adding a middleware to print the HTTP request body?

I am using a middleware to print the HTTP request body to avoid print statements in every function. However, there is no response from fastapi when running the client code. The server is simplified to the code below: import typing import…
Hobin C.
  • 671
  • 1
  • 8
  • 17
4
votes
4 answers

Response file stream from S3 FastAPI

I am trying to return a response of a picture from S3. In StreamingResponse.stream_response I see, that chunks are read from the stream and sent to the socket. But on the other side, nothing comes. import uvicorn from fastapi import FastAPI from…
Soltanat
  • 51
  • 1
  • 3
4
votes
0 answers

How to pass parameters to a FastAPI middleware and influence its processing logic?

we know that through request.state we can pass in some custom data to handlers from middleware's before-handle process and thus influence its behaviour, I'm currently wondering how can a handler influence the middleware's logic after it. My specific…
AdamHommer
  • 657
  • 7
  • 22
4
votes
1 answer

Fastapi database test isolation with the starlette configuration

how to exactly configure a database for test purposes. I am doing something wrong, I have read the starlette configuration on the website but I think I did not understand it correctly. I tried to build it but I get the following error…
Liam
  • 41
  • 1
  • 3
4
votes
1 answer

Fast api, sending json response in web socket

I have a small WebSocket built with fast api. I'm trying to receive a message from the client, process it, and return a JSON response. @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() while…
user12177026
4
votes
3 answers

FastAPI: How to download bytes through the API

Is there a way to download a file through FastAPI? The files we want are located in an Azure Datalake and retrieving them from the lake is not an issue, the problem occurs when we try to get the bytes we get from the datalake down to a local…
Markus
  • 83
  • 1
  • 7
4
votes
0 answers

Is there a way to retrieve the client IP address using a Python backend server

the setup in question looks like this: my web app implemented using fastapi and deployed using gunicorn and the uvicorn worker class, is behind an nginx proxy on the same host with IP address 172.31.x.x (and behind other remote appliances like VPN…
nskalis
  • 2,232
  • 8
  • 30
  • 49
4
votes
2 answers

How to handle JSON request body with the Starlette framework

I am moving my API framework from an older version of ApiStar to Starlette and am having trouble correctly accessing the HTTP body which, in this case, is a JSON payload, in the functions that I am routing to. This is what was working for me with…
jpjenk
  • 459
  • 2
  • 8
  • 14
3
votes
1 answer

Excluding pydantic model fields only when returned as part of a FastAPI call

Context I have a very complex pydantic model with a lot of nested pydantic models. I would like to ensure certain fields are never returned as part of API calls, but I would like those fields present for internal logic. What I tried I first tried…
rbhalla
  • 869
  • 8
  • 32
3
votes
2 answers

How to re-route requests to a different URL/endpoint in FastAPI?

I am trying to write a middleware in my FastAPI application, so that requests coming to endpoints matching a particular format will be re-routed to a different URL, but I am unable to find a way to do that since request.url is read-only. I am also…
Aniket Tiratkar
  • 798
  • 6
  • 16