How pydantic handles if request body is None #11559
Replies: 2 comments 1 reply
-
Hi!
|
Beta Was this translation helpful? Give feedback.
-
I am expecting a request body as Items (Pydantic model). Also I am using an exception handler before the request reaches to my endpoint I want to check if request body is None or not how can I achieve this because exception handler is not catching is by itself.
|
Beta Was this translation helpful? Give feedback.
-
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})
Beta Was this translation helpful? Give feedback.
All reactions