Issue getting body params without using pydantic model
See original GitHub issueI’ve followed the instructions in #895 and in the docs but I must be doing something silly because this doesn’t work:
@router.post("/posts", tags=["posts"], status_code=201)
async def create_post(url: str = Body(...)):
print(url)
return []
It returns {"detail":[{"loc":["body","url"],"msg":"str type expected","type":"type_error.str"}]}
with the body { "url": "hello there" }
but the following works using a pydantic model:
class CreatePost(BaseModel):
url: str
@router.post("/posts", tags=["posts"], status_code=201)
async def create_post(body: CreatePost):
print(body.url)
return []
Any ideas?
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
fastapi request body without pydantic - Tripnologies
The project uses pydantic to map incoming request body data into a storage ... Issue getting body params without using pydantic model.
Read more >fastapi - How to read body as any valid json? - Stack Overflow
A parameter with the default Body gets all the payload that doesn't match passed Pydantic-typed parameters (the whole payload in our case) ...
Read more >Request Body - FastAPI
Without Pydantic If you don't want to use Pydantic models, you can also use Body parameters. See the docs for Body -...
Read more >Parse JSON-encoded query strings in FastAPI
How to parse query string to pydantic models with FastAPI Depends() and dynamic function signatures.
Read more >A Guide to FastAPI Request Body Using Pydantic BaseModel
Learn how to use FastAPI Request Body by leveraging the power of Pydantic BaseModel class to convert and validate incoming requests.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Short answer: Adding
embed=True
to url will workLong answer:
url
as the only Body variable, FastAPI thought of it as describing the whole Body structure{ "url": "hello there" }
, if you passed"hello there"
, it would have workedbody
as the only Body variable, FastAPI thought of it as describing the whole Body structure@tiangolo I have a view that the default should be other way round. To have only one variable describe the whole body structure, we must have a flag like
whole_body=True
added.Thanks @gautamsinghania95 for the suggestion, but I would expect that for most of the use cases people would use a single variable expecting it to be the top body. And having to add additional code to make it work like that would end up being quite cumbersome.
I agree that the fact that it behaves differently with respect to one specific parameter depending on if there are other body parameters or not is not ideal, but here’s one of those points where I would trade the simplicity for most use cases in exchange of a little inconsistency for these corner cases.
And either way, both options are achievable with
embed=True
.On the other hand, changing the default behavior would break many live apps, for not a very big improvement. So I think I’ll leave it as it is.