fastapi

FastAPI unit test returns 422

FastAPI unit test returns 422 Question: I am new to python and FastAPI and writing some test code for FastAPI endpoints. I have the following function for unit testing, but when I run it I get back status code 422. Not sure what I am doing wrong: def test_create_event(self): my_date = datetime.date(2023, 6, 15) data …

Total answers: 1

What is the difference between Security and Depends in FastAPI?

What is the difference between Security and Depends in FastAPI? Question: This is my code: from fastapi import FastAPI, Depends, Security from fastapi.security import HTTPBearer bearer = HTTPBearer() @app.get("/") async def root(q = Security(bearer)): return {‘q’: q} @app.get("/Depends") async def root(q = Depends(bearer)): return {‘q’: q,} Both routes give precisely the same result and act …

Total answers: 1

SQL query to create a nested pydantic structure

SQL query to create a nested pydantic structure Question: While working with SQLAlchemy 2.x and FastAPI, I ran into the problem of creating a nested structure for pydantic serialization. The specific essence of the problem lies in the implementation of the sql query. The fact is that the received data comes as a single tuple …

Total answers: 1

How to assign the url of the uploaded photo from AWS to database(postgresql) model in the same endpoint

How to assign the url of the uploaded photo from AWS to database(postgresql) model in the same endpoint Question: Below that there is an endpoint that uploads a photo to aws-s3 and return the url of the photo. This works well. session1 = boto3.Session( aws_access_key_id=’XXXXXXXXXX’, aws_secret_access_key=’XXXXXXXXX’ ) S3_BUCKET_NAME = "XXXXX" s3 = session1.resource("s3") bucket = …

Total answers: 2

Finding a specific element in nested Array MongoDB

Finding a specific element in nested Array MongoDB Question: DB Schema [{ "_id": 1, "name": "city1", "districts": [ { "id": 5, "name": "district 1", "neighborhoods": [ { "id": 309, "name": "neighborhood 1" } ] }, { "id": 6, "name": "district 2", "neighborhoods": [ { "id": 52280, "name": "neighborhood 2" } ] } }, { "_id": …

Total answers: 1

Raise HTTPException not being called

Raise HTTPException not being called Question: I have the following code: @events_router.delete("/events/{event_id}", status_code=204) def delete_event(*, event_id: int) -> None: for event in EVENTS: if event[‘id’] == event_id: del EVENTS[event_id-1] print("event id = " + str(event_id) + " deleted!") break raise HTTPException( status_code=404, detail=f"Event with ID {event_id} not found" ) If I add an event and …

Total answers: 1

How to return the average value using fastapi & pydantic

How to return the average value using fastapi & pydantic Question: I am new to fasts-api and python as I am a mobile developer. At the moment I managed to get this response from my API (please look at averageLevel which is an array at the moment): [ { "user_id": 139, "event_date": "2023-03-20T12:18:17", "public": 1, …

Total answers: 1

WebSockets in FastAPI – ConnectionClosedOK: received 1000 (OK)

WebSockets in FastAPI – ConnectionClosedOK: received 1000 (OK) Question: I have 3 clients, who periodically send data to my server. I use the example from FastAPI’s documentation. This is my server code: class ConnectionManager: def __init__(self): self.active_connections: list[WebSocket] = [] async def connect(self, websocket: WebSocket): await websocket.accept() self.active_connections.append(websocket) def disconnect(self, websocket: WebSocket): self.active_connections.remove(websocket) async def …

Total answers: 1

Using Python FastAPI. Web server running apache how to serve static folder over HTTPS

Using Python FastAPI. Web server running apache how to serve static folder over HTTPS Question: When using FastAPI all files in my static folder are served over HTTP and not HTTPS. To be more clear they’re not being severed over my domain but the localhost of my webserver. I’m assuming this is something between how …

Total answers: 1

FastAPI StreamingResponse not streaming with generator function

FastAPI StreamingResponse not streaming with generator function Question: I have a relatively simple FastAPI app that accepts a query and streams back the response from ChatGPT’s API. ChatGPT is streaming back the result and I can see this being printed to console as it comes in. What’s not working is the StreamingResponse back via FastAPI. …

Total answers: 4