Merge Path + Query Parameters in BaseModel
See original GitHub issueHi all,
I just started using fastapi and I’m struggling to understant how to use the BaseModel on both path + query parameters.
my endpoint:
rs = client.get("/data/a/b/c")
# possibly
rs = client.get("/data/a/b/c?sort=true&page=1")
On the code below is there a way to make the item return all the optional values declared in the Model?
class Item(BaseModel):
sort: Optional[str] = ""
page: Optional[int] = 1
per_page: Optional[int] = 20
search_term: Optional[str] = ""
@app.get("/data/{l1}/{l2}/{l3}")
def d_tree(
l1: str,
l2: str,
l3: str,
item: Optional[Item] = None):
print(l1, l2, l3) # l1 l2 l3 ok
print(item) # None
This would throw:
@app.get("/data/{l1}/{l2}/{l3}")
def d_tree(*,
l1: str,
l2: str,
l3: str,
item: Item):
#{'detail': [{'loc': ['body'], 'msg': 'field required', 'type': 'value_error.missing'}]}
I’m wondering if It’s possible to get all my variables inside the Model like this:
class Item(BaseModel):
l1: str
l2: str
l3: str
sort: Optional[str] = ""
page: Optional[int] = 1
per_page: Optional[int] = 20
search_term: Optional[str] = ""
@app.get("/data/{l1}/{l2}/{l3}")
def d_tree(item: Item):
toil.phead(item)
return []
Result
{'detail': [{'loc': ['body'], 'msg': 'field required', 'type': 'value_error.missing'}]}
Expect
{
'l1': 'a',
'l2': 'b',
'l3': 'c',
'page': 1,
'per_page': 20,
'search_term': '',
'sort': ''
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Body - Multiple Parameters - FastAPI
First, of course, you can mix Path , Query and request body parameter ... fastapi import FastAPI, Path from pydantic import BaseModel app...
Read more >How to implement dynamic API filtering using query ... - Mindee
When it comes to API filtering, the most intuitive way of doing this is to use query parameters. Query parameters only having a...
Read more >Swagger: Combine Path and Query Parameters in Same ...
Can you use both a path parameter and a query parameter in Swagger 2.0 or 3.0? For example, given the following base URL...
Read more >2.0 Breaking Changes | FeathersVuex
Using custom query parameters. There are two places where the query operators have to be allowed. In the Feathers Client (for the actions):...
Read more >tiangolo/fastapi - Gitter
Does anyone know how to use a pydantic BaseModel as query parameter? I can't find this information in the documentation. I would like...
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
Thanks for the help here @jrversteegh! 🚀
And thanks @CrashLaker for coming back to close the issue.
np and you’re welcome.