update: sync code example

This commit is contained in:
Yuki Watanabe 2024-05-16 09:40:25 +09:00
parent 9bd061c33b
commit 39164bd6f3
1 changed files with 6 additions and 2 deletions

View File

@ -146,6 +146,8 @@ $ pip install fastapi
- `main.py` を作成し、以下のコードを入力します:
```Python
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@ -157,7 +159,7 @@ def read_root():
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
```
@ -167,6 +169,8 @@ def read_item(item_id: int, q: str = None):
`async` / `await`を使用するときは、 `async def`を使います:
```Python hl_lines="7 12"
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@ -178,7 +182,7 @@ async def read_root():
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
async def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
```