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
7
votes
1 answer

FastAPI UploadFile is slow compared to Flask

I have created an endpoint, as shown below: @app.post("/report/upload") def create_upload_files(files: UploadFile = File(...)): try: with open(files.filename,'wb+') as wf: wf.write(file.file.read()) …
user3656746
  • 71
  • 1
  • 2
6
votes
1 answer

How to create a FastAPI endpoint that can accept either Form or JSON body?

I would like to create an endpoint in FastAPI that might receive (multipart) Form data or JSON body. Is there a way I can make such an endpoint accept either, or detect which type of data is receiving?
STY
  • 73
  • 1
  • 6
6
votes
1 answer

How do I convert a torch tensor to an image to be returned by FastAPI?

I have a torch tensor which I need to convert to a byte object so that I can pass it to starlette's StreamingResponse which will return a reconstructed image from the byte object. I am trying to convert the tensor and return it like so: def…
6
votes
2 answers

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

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…
Praveen K M
  • 257
  • 6
  • 16
6
votes
2 answers

Getting Query Parameters as Dictionary in FastAPI

I spent last month learning Flask, and am now moving on to Pyramid and FastAPI. One of the requirements for my application is to get all the query parameters in a dictionary (there are multiple combinations, and I can assume all keys are unique and…
John Heyer
  • 711
  • 1
  • 6
  • 18
6
votes
2 answers

Starlette's url_for doesn't create links with https scheme behind Nginx (via uvicorn)

I've tried everything: @Starlette: routes = [ Mount("/static/", StaticFiles(directory=parent+fs+"decoration"+fs+"static"), name="static"), Route(....), …
Lopofsky
  • 518
  • 6
  • 15
5
votes
1 answer

How to add background tasks when request fails and HTTPException is raised in FastAPI?

I was trying to generate logs when an exception occurs in my FastAPI endpoint using a Background task as: from fastapi import BackgroundTasks, FastAPI app = FastAPI() def write_notification(message=""): with open("log.txt", mode="w") as…
Amogh Mishra
  • 1,088
  • 1
  • 16
  • 25
5
votes
1 answer

How to modify the scope field of a Request object in Starlette/FastAPI for unit testing

Let's say I have a FastAPI route with a basic get request that looks something like: @router.get("/reports") async def get_reports(request: Request) -> Dict: return and I want to test it using: def test_method_can_access_request_fields(): …
John Kealy
  • 1,503
  • 1
  • 13
  • 32
5
votes
1 answer

How to save and reuse tokens with authlib

I'm very beginner with authlib and trying to understand its concepts. I try to understand, how can I save and reuse fetched tokens with authlib. I created small FastAPI project: from fastapi import FastAPI from starlette.config import Config from…
catscoolzhyk
  • 675
  • 10
  • 29
5
votes
1 answer

Overriding FastAPI's HTTPException response body

I'm currently writing a few end points for an API in fastAPI. I'm defining classes that extend fastapi's HTTPException. The problem is that HTTPException returns a response body with an attribute called detail which will either be a string or a json…
neo-devnull
  • 51
  • 1
  • 3
5
votes
1 answer

How does FastAPI's application mounting works?

For certain reasons, we have chosen the FastAPI, in order to use it as back-end tier of our multi-module production. One of its attractive features is sub application, that helps us to separate different modules with intention of making it more…
5
votes
1 answer

Authentication verification in Python based GraphQL Server using FastAPI

I am having trouble implementing authentication verification in an GraphQL Server built with FastAPI. Before, we were using REST but now we are switching to GraphQL and I am wondering how I can implement this. Before, we had different routers and…
jmandt
  • 386
  • 5
  • 13
5
votes
1 answer

Alembic autogenerate empty migration file

I'm trying to connect the alembic library to the databases and sqlalchemy libraries. As a guide, I use this example link My projects file: db.py from databases import Database from sqlalchemy import MetaData, create_engine DATABASE_URL =…
Jekson
  • 2,892
  • 8
  • 44
  • 79
5
votes
3 answers

How to programmatically instantiate Starlette's Request with a body?

I have a project with some APIs using FastAPI in which I need to call one of the API functions inside the project. The API function using FastAPI and Starlette looks like this @router.put("/tab/{tab_name}", tags=["Tab CRUD"]) async def…
Ghasem
  • 14,455
  • 21
  • 138
  • 171
5
votes
1 answer

What happens to existing awaits when WebSocket.close is called

If I open a Starlette/FastAPI WebSocket, what happens to any coroutines currently waiting to receive data from the client if I close the WebSocket from outside the coroutine? Does the call to receive raise an exception or does the coroutine sit in…
drhagen
  • 8,331
  • 8
  • 53
  • 82
1 2
3
17 18