question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue getting body params without using pydantic model

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
gautamsinghania95commented, Mar 10, 2020

Short answer: Adding embed=True to url will work

@router.post("/posts", tags=["posts"], status_code=201)
async def create_post(url: str = Body(..., embed=True)):
    print(url)
    return []

Long answer:

  • With only one variable as the Body, FastAPI thinks of that as describing the whole body
  • In the first scenario, with url as the only Body variable, FastAPI thought of it as describing the whole Body structure
  • Since it was a string, a map failed
  • In the body, instead of { "url": "hello there" }, if you passed "hello there", it would have worked
  • In the second scenario, with body as the only Body variable, FastAPI thought of it as describing the whole Body structure
  • Since it was a Pydantic BaseModel, a map worked

@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.

2reactions
tiangolocommented, Jun 6, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found