fastapi

How to disable application/json in Swagger UI autodocs of a FastAPI application?

How to disable application/json in Swagger UI autodocs of a FastAPI application? Question: My API can return only a file: @router.get( "/partners/{partner_id}/rsr-requests/{rsr_request_id}/{document_path}", responses={200: {"content": {"application/octet-stream": {}}, "description": "Файл"}} ) async def download_rsr_document(…): pass But in Swagger UI, I can see that application/json still remains. How to disable it? Asked By: Альберт Александров || Source Answers: …

Total answers: 2

How to return a list type in FastAPI response?

How to return a list type in FastAPI response? Question: My objective is return a list of tags as response. I have a Tag schema like this in my FastAPI app: class Tag(BaseModel): nome: str class Config: from_attributes = True class TagList(BaseModel): tags: List[Tag] = [] class Config: from_attributes = True I wanted return the …

Total answers: 1

FastAPI.Path : TypeError: Path() missing 1 required positional argument: 'default'

FastAPI.Path : TypeError: Path() missing 1 required positional argument: 'default' Question: From tutorials I’ve seen the below used. However, when I try to replicate (running Docker container locally) I observe the below error. @app.get("/get-student/{student_id}") # path parameters and query parameters have no overlap def get_student(student_id: int = Path( description="student ID", gt=0 #minimum ID = 1, …

Total answers: 1

Migrate PostgresDsn.build from pydentic v1 to pydantic v2

Migrate PostgresDsn.build from pydentic v1 to pydantic v2 Question: I have simple Config class from FastAPI tutorial. But it seems like it uses old pydantic version. I run my code with pydantic v2 version and get a several errors. I fix almost all of them, but the last one I cannot fix yet. This is …

Total answers: 5

How to make a JSON POST request from Java client to Python FastAPI server?

How to make a JSON POST request from Java client to Python FastAPI server? Question: I send a post request from a java springboot application like this: String requestBody = gson.toJson(sbert); System.out.println(requestBody); // If I print this, and use this in postman it works! HttpRequest add_request = HttpRequest.newBuilder() .uri(URI.create("http://localhost:4557/sbert_similarity")) .POST(HttpRequest.BodyPublishers.ofString(requestBody)) .header("Content-Type", "application/json") .build(); HttpResponse<String> response …

Total answers: 1

How to raise custom exceptions in a FastAPI middleware?

How to raise custom exceptions in a FastAPI middleware? Question: I have a simple FastAPI setup with a custom middleware class inherited from BaseHTTPMiddleware. Inside this middleware class, I need to terminate the execution flow under certain conditions. So, I created a custom exception class named CustomError and raised the exception. from fastapi import FastAPI, …

Total answers: 2

How to delete rows by a condition using sqlalmchemy and fastapi?

How to delete rows by a condition using sqlalmchemy and fastapi? Question: I’m trying to delete some rows that a ‘null’ in a Postgresql db by this: async def delete_empty_batches(): query = Batches.delete().where(Batches.c.acquired_by_warehouse_date is None) database.execute(query).fetchall() s = Batches.select() return await database.execute(s).fetchall() But nothing happens. I have tried to make a query in pgAdmin and …

Total answers: 2

Can I return 400 error instead of 422 error

Can I return 400 error instead of 422 error Question: I validate data using Pydantic schema in my FastAPI project and if it is not ok it returns 422. Can I change it to 400? Asked By: Konstantinos || Source Answers: Yes, you can. For example, you can apply the next exception handler: from fastapi …

Total answers: 1