How pydantic handles if request body is None #11559
Unanswered
ajay-sudosu
asked this question in
Questions
Replies: 1 comment
-
Hi!
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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