fastapi

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

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

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