Replies: 11 comments 1 reply
-
I love being able to answer my own questions. Apparently I missed the dependencies section of the documentation, which basically does exactly what I want: https://fastapi.tiangolo.com/tutorial/dependencies/first-steps/ Hope that helps anyone coming across this question with the same issue. |
Beta Was this translation helpful? Give feedback.
-
Thanks for reporting back and saying how you solved it 😀🚀 |
Beta Was this translation helpful? Give feedback.
-
trying to solve the same, but the link seems to be broken :) |
Beta Was this translation helpful? Give feedback.
-
@inikolaev https://fastapi.tiangolo.com/tutorial/dependencies/ this link should work! |
Beta Was this translation helpful? Give feedback.
-
Thank you! I have found it myself, but now I have a question: is it possible to write a generic function which will parse whatever query parameter it's applied to? Something like: async def parse_list(request: Request):
# extract all query parameters with name specified in `name`
# parse and return array
async def route(param: List[str] = Depends(parse_list)):
pass Is it possible to somehow get the name of the parameter dependency is being applied to? |
Beta Was this translation helpful? Give feedback.
-
@inikolaev You can probably define it in the additional responses: https://fastapi.tiangolo.com/advanced/additional-responses/ |
Beta Was this translation helpful? Give feedback.
-
I'm not really sure how this is relevant. My question was about how to create a custom query parameters parser, similar to Right now it's possible to achieve with async def parse_list(param_name: str):
def parse(request: Request):
return request.query_params[param_name].split(',')
return parse
async def route(param: List[str] = Depends(parse_list('param'))):
^^^^^ ^^^^^
pass But this way I basically have to duplicate parameter name twice: first in the formal parameter of the route function and then as an actual parameter when calling My question was whether it's possible to retrieve the name of the formal parameter in the route function, instead of passing it as string manually. Does it make more sense now? |
Beta Was this translation helpful? Give feedback.
-
Any updates on this, I am struggling to find any straightforward way to parse nested query params from clients that you use something like https://www.npmjs.com/package/qs to stringify query params. @inikolaev Did you find any way around your problem ? |
Beta Was this translation helpful? Give feedback.
-
My use-case was simple enough, so I simply passed multiple query parameters with same name, which the library can handle: @api_router.get(path='/items')
async def get(status: List[ItemStatus] = Query([])):
... |
Beta Was this translation helpful? Give feedback.
-
I implemented @inikolaev's suggestion which works great, but the parameter does not appear in the OpenAPI spec. Is there any way to have it appear in the OpenAPI spec (preferably using a decorator or something in the same location, not overriding the whole spec). |
Beta Was this translation helpful? Give feedback.
-
I ran into the same issue. In my case, our endpoint was being called with naive datetimes and I wanted to coerce them to UTC without a lot of ceremony. I solved it with pydantic's
|
Beta Was this translation helpful? Give feedback.
-
Description
I'm currently migrating an API to FastAPI and I'm trying to prevent too many changes regarding how you call the API. To filter on the old API you'd pass a json serialized complex dict(with nested dicts and lists in it) as a query parameter and this would then be deserialized and parsed to create the filter. I can make this work in FastAPI as well, but this means I have to call
json.loads(filter_)
in every route that uses it, and it kind of feels like there is a way to declare a custom type that does this for me.On a similar note: the old API had comma separated lists. For example:
?columns=id,name,email
. If I define something as a list in FastAPI right now I have to pass it this:?columns=id&columns=name&columns=email
which seems really verbose for my usecase(I can see it being useful if your data would potentially contain a "," though), so I'd like to write a custom parser for that as well(although iirc OpenAPI has support for this way of defining lists already somewhere).Could you help point me in the right direction on how to solve these issues?
Beta Was this translation helpful? Give feedback.
All reactions