python用fastapi获取get参数

在FastAPI中获取GET参数,可以使用FastAPI提供的`Query`函数。 示例代码:

```python
from fastapi import FastAPI
from fastapi import Query

app = FastAPI()

@app.get("/items/")
async def read_items(skip: int = 0, limit: int = Query(default=100, gt=0, le=1000)):
    """
    skip: int参数是路径参数,不需要使用Query函数进行声明
    limit: int参数是使用了Query函数进行声明
           default表示默认值为100;
           gt表示大于,le表示小于等于
    """
    return {"skip": skip, "limit": limit}
```

上述代码演示了如何设置默认的参数值,以及如何限制参数的范围。

相关代码参考