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

Viewed 53

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

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
Related