how to accept the part dict of the json body as the dict rather than the entire json body #6911
-
|
OS: Ubuntu 18.04 LTS This is part of my code @router.post("/register")
async def register(user:dict = Body(...)):
"the code"However, when I sent the body like below {
"user":{
"name":"john",
"age":14,
"position": "engineer"
}
}I wanted the param user to be accepted as {
"name":"john",
"age":14,
"position": "engineer"
}but it turned out to be the entire body {
"user":{
"name":"john",
"age":14,
"position": "engineer"
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
|
... filter the dict yourself , in your code @router.post("/register")
async def register(user:dict = Body(...)):
print(user["user"])you could also do a model ( pydantic ) for an input validation of the json you can close your issue , thank you |
Beta Was this translation helpful? Give feedback.
-
Well, even if there has been no perfect method, I find a somewhat elegant way to solve. add a optional param that cannot be captured to the function and the param user will be accepted as I want @router.post("/register")
async def register(user:dict = Body(...),_:Optional[str] = Body("")):
"the code" |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @raphaelauv ! 👏 🙇 I'm not sure I understood what you needed but I'm glad you solved your use case! Thanks for reporting back and closing the issue 👍 ☕
|
Beta Was this translation helpful? Give feedback.
-
|
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
Beta Was this translation helpful? Give feedback.
Well, even if there has been no perfect method, I find a somewhat elegant way to solve.
add a optional param that cannot be captured to the function and the param user will be accepted as I want