tiangolo / fastapi

FastAPI framework, high performance, easy to learn, fast to code, ready for production
https://fastapi.tiangolo.com/
MIT License
73.34k stars 6.18k forks source link

How pydantic handles if request body is None #11558

Closed ajay-sudosu closed 2 months ago

ajay-sudosu commented 2 months ago

Privileged issue

Issue Content

Suppose I have a model: class User(Basemodel): name: str age: str

And I have an endpoint something like: @app.post("/user", description="User add") def add_user(add_user_data: User): so_something()

I am expecting a request body with the user details as show above in pydantic model Question is how should I handle the case when request body is None

Note: I am using fastapi add_exception_handler() function to handle the incoming request before reaching it to the endpoint some thing like:

def handle_pydantic_validations(request: Request, exc: ValidationError): errors = exc.args[0] error_list = [{error["loc"][1]: error["msg"]} for error in errors] return make_response(status_code=status.HTTP_400_BAD_REQUEST, body={"body": error_list, "status_code": status.HTTP_400_BAD_REQUEST})