GET 操作

FastAPI入門

Matt Eckerle

Software and Data Engineering Leader

GET の復習

HTTP プロトコルには複数の操作があります

  • 最も一般的なのは GET

例: https://www.google.com:80/search?q=fastapi

GET リクエストの主要要素:

  • ホスト 例: www.google.com
  • ポート 例: 80(既定)
  • パス 例: /search
  • クエリ文字列 例: ?q=fastapi
FastAPI入門

FastAPI の GET 操作

最も簡単な FastAPI アプリ:

from fastapi import FastAPI

# アプリを作成 app = FastAPI()
# ルートへの GET を処理 @app.get("/") def root(): return {"message": "Hello World"}
1 https://fastapi.tiangolo.com/tutorial/first-steps/
FastAPI入門

cURL Web クライアントの使用

主な cURL オプション:

$ curl -h
Usage: curl [options...] <url>
 -v, --verbose               Make the operation more talkative
 -H, --header <header/@file> Pass custom header(s) to server
 -d, --data <data>           HTTP POST data

使用例:

$ curl http://localhost:8000
{"message":"Hello World"}
FastAPI入門

クエリパラメータ

新しいエンドポイント:

  • パス: "/hello"
  • クエリパラメータ: "name"
    • 既定値: "Alan"
@app.get("/hello")
def hello(name: str = "Alan"):
    return {"message": f"Hello {name}"}

リクエストに名前なし: 名前を指定しない curl リクエストの端末。応答は "Hello Alan."。

リクエストに名前あり: 名前を指定した curl リクエストの端末。応答は "Hello Steve."。

FastAPI入門

練習しましょう!

FastAPI入門

Preparing Video For Download...