doc: update docs/fastapi.md #422

This commit is contained in:
jaywcjlove 2023-09-04 11:36:09 +08:00
parent 4d21e9f1cf
commit b3e8be65f6
2 changed files with 22 additions and 11 deletions

View File

@ -1,3 +1,3 @@
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em"> <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" fill="currentColor" height="1em" width="1em">
<path d="M12 0C5.375 0 0 5.375 0 12c0 6.627 5.375 12 12 12 6.626 0 12-5.373 12-12 0-6.625-5.373-12-12-12zm-.624 21.62v-7.528H7.19L13.203 2.38v7.528h4.029L11.376 21.62z"/> <path d="M12 0C5.375 0 0 5.375 0 12c0 6.627 5.375 12 12 12 6.626 0 12-5.373 12-12 0-6.625-5.373-12-12-12zm-.624 21.62v-7.528H7.19L13.203 2.38v7.528h4.029L11.376 21.62z"/>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 266 B

After

Width:  |  Height:  |  Size: 286 B

View File

@ -8,7 +8,7 @@ FastAPI 是一个用于构建 API 的现代、快速(高性能)的 web 框
### 最小程序 ### 最小程序
下面代码会直接启动http服务也可以使用uvicorn main:app --reload 下面代码会直接启动http服务也可以使用 `uvicorn main:app --reload`
```python ```python
from fastapi import FastAPI from fastapi import FastAPI
@ -28,18 +28,20 @@ if __name__ == '__main__':
### 查询参数 ### 查询参数
<!--rehype:wrap-class=col-span-2 row-span-2--> <!--rehype:wrap-class=col-span-2 row-span-2-->
带默认值的查询参数 <http://127.0.0.1:8000/items/?skip=0&limit=2> 带默认值的查询参数
```python ```python
# http://127.0.0.1:8000/items/?skip=0&limit=2
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}] fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
@app.get("/items/") @app.get("/items/")
async def read_item(skip: int = 0, limit: int = 10): async def read_item(skip: int = 0, limit: int = 10):
return fake_items_db[skip: skip + limit] return fake_items_db[skip: skip + limit]
``` ```
可选查询参数 <http://127.0.0.1:8000/items/1?q=admin> 可选查询参数
```python ```python
# http://127.0.0.1:8000/items/1?q=admin
from typing import Union from typing import Union
@app.get("/items/{item_id}") @app.get("/items/{item_id}")
async def read_item(item_id: str, q: Union[str, None] = None): async def read_item(item_id: str, q: Union[str, None] = None):
@ -48,9 +50,12 @@ async def read_item(item_id: str, q: Union[str, None] = None):
return {"item_id": item_id} return {"item_id": item_id}
``` ```
多路径多查询参数 <http://127.0.0.1:8000/users/1/items/2> or <http://127.0.0.1:8000/users/1/items/2?q=query&short=true> 多路径多查询参数
```python ```python
# http://127.0.0.1:8000/users/1/items/2
# or
# http://127.0.0.1:8000/users/1/items/2?q=query&short=true
@app.get("/users/{user_id}/items/{item_id}") @app.get("/users/{user_id}/items/{item_id}")
async def read_user_item( async def read_user_item(
user_id: int, item_id: str, q: Union[str, None] = None, short: bool = False user_id: int, item_id: str, q: Union[str, None] = None, short: bool = False
@ -65,9 +70,10 @@ async def read_user_item(
return item return item
``` ```
必需查询参数 <http://127.0.0.1:8000/items/123?needy=yes> 必需查询参数
```python ```python
# http://127.0.0.1:8000/items/123?needy=yes
@app.get("/items/{item_id}") @app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str): async def read_user_item(item_id: str, needy: str):
item = {"item_id": item_id, "needy": needy} item = {"item_id": item_id, "needy": needy}
@ -76,33 +82,37 @@ async def read_user_item(item_id: str, needy: str):
### 路径参数 ### 路径参数
最基本的路径参数 <http://127.0.0.1:8000/items/1> 最基本的路径参数
```python ```python
# <http://127.0.0.1:8000/items/1>
@app.get("/items/{item_id}") @app.get("/items/{item_id}")
async def read_item(item_id): async def read_item(item_id):
return {"item_id": item_id} # item_id自定义 return {"item_id": item_id} # item_id自定义
``` ```
多个路径参数 <http://127.0.0.1:8000/items/1/2> 多个路径参数
```python ```python
# <http://127.0.0.1:8000/items/1/2>
@app.get("/items/{item_id}/{user_id}") @app.get("/items/{item_id}/{user_id}")
async def read_item(item_id, user_id): async def read_item(item_id, user_id):
return {"item_id": item_id, "user_id": user_id} return {"item_id": item_id, "user_id": user_id}
``` ```
有类型的路径参数 <http://127.0.0.1:8000/items/1> 有类型的路径参数
```python ```python
# <http://127.0.0.1:8000/items/1>
@app.get("/items/{item_id}") @app.get("/items/{item_id}")
async def read_item(item_id: int): async def read_item(item_id: int):
return {"item_id": item_id} return {"item_id": item_id}
``` ```
文件路径参数 <http://127.0.0.1:8000/file//home/my/my.txt> 文件路径参数
```python ```python
# <http://127.0.0.1:8000/file//home/my/my.txt>
@app.get("/file/{file_path:path}") @app.get("/file/{file_path:path}")
async def read_item(file_path): async def read_item(file_path):
return {"file_path": file_path} return {"file_path": file_path}
@ -140,6 +150,7 @@ curl -X 'POST' \
``` ```
### 查询参数和字符串校验 ### 查询参数和字符串校验
<!--rehype:wrap-class=col-span-2-->
```python ```python
from fastapi import Query from fastapi import Query