Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle path parametes with hyphen(-) in a FastAPI application?

Tags:

fastapi

How to handle path parametes with hyphen(-) in FastAPI, as python does not allow hyphen in identifiers?

like image 397
Piyush Gupta Avatar asked Oct 19 '25 10:10

Piyush Gupta


1 Answers

You can use alias in your definitions. It’s all documented here

As stated in the comments below, if the links stops working here is the code:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, alias="item-query")):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
like image 73
JarroVGIT Avatar answered Oct 22 '25 06:10

JarroVGIT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!