0Pricing
FastAPI Backend Development Bootcamp · レッスン

パスパラメーターとクエリパラメーター

パスパラメーターを使ってURLから動的なデータを取得し、省略可能なクエリパラメーターを処理する方法を理解します。

「パスパラメーターとクエリパラメーター」はCoddyKit上の無料FastAPI Backend Development Bootcampレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFastAPI Backend Development Bootcamp学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 FastAPI Backend Development Bootcampコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Dynamic URLs for APIs

APIs often carry data right in the URL — fetching a product by ID or filtering a list. FastAPI gives you clean ways to capture that. Let's dig in.

Meet Path Parameters

Path parameters are URL segments that pin down a specific resource, written in curly braces like /items/{item_id}. Perfect for one exact item.

Your First Path Parameter

Match a path segment to a function arg by name: /items/{item_id} maps to item_id. The type hint int auto-converts it.

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id, "message": "Here is your item!"}

Power of Type Hints

Type hints like item_id: int drive validation. Hit /items/abc and FastAPI returns a clean error automatically. Use str, int, float, bool, UUID.

Handling Multiple Paths

You can take several path parameters in one route. FastAPI matches them by name, so the order in your function signature doesn't matter.

from fastapi import FastAPI

app = FastAPI()

@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(user_id: int, item_id: str):
    return {"user_id": user_id, "item_id": item_id}

What are Query Parameters?

Query parameters are key-value pairs after a ?, like /items?skip=0&limit=10. They are optional — ideal for filtering, paging, or sorting.

Your First Query Parameter

Any function arg that is not in the path becomes a query parameter automatically. Here, skip and limit are typed integers.

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/")
async def read_items(skip: int = 0, limit: int = 10):
    return {"skip": skip, "limit": limit}

Optional Queries & Defaults

Make a query parameter optional by giving it a default. For a None default, type it as Optional[str] so FastAPI knows it may be missing.

from typing import Optional
from fastapi import FastAPI

app = FastAPI()

@app.get("/products/")
async def read_products(q: Optional[str] = None, page_size: int = 20):
    results = {"page_size": page_size}
    if q:
        results.update({"search_query": q})
    return results

Path + Query Power

Combine both: path parameters are required, query parameters stay optional with defaults. FastAPI sorts out which is which seamlessly.

from typing import Optional
from fastapi import FastAPI

app = FastAPI()

@app.get("/users/{user_id}/orders/")
async def get_user_orders(user_id: int, status: Optional[str] = None, limit: int = 10):
    return {"user_id": user_id, "status": status, "limit": limit, "message": "User orders fetched."}

Quick Check on Parameters

Consider the following FastAPI endpoint:

from typing import Optional
from fastapi import FastAPI

app = FastAPI()

@app.get("/books/{book_id}")
async def get_book(book_id: int, author: str = "Unknown", published_year: Optional[int] = None):
    return {
        "book_id": book_id,
        "author": author,
        "published_year": published_year
    }

Which of the following URLs would successfully call this endpoint and return a published_year?

Recap: Dynamic API Routes

Great work! You made routes dynamic with path parameters for required IDs, query parameters for optional filters, and type hints for free validation.

よくある質問

「パスパラメーターとクエリパラメーター」レッスンは無料ですか?

はい。「パスパラメーターとクエリパラメーター」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、FastAPI Backend Development Bootcampコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 FastAPI Backend Development Bootcampコースには全4レッスンが含まれています。

「パスパラメーターとクエリパラメーター」で何を学びますか?

パスパラメーターを使ってURLから動的なデータを取得し、省略可能なクエリパラメーターを処理する方法を理解します。 ブラウザで直接実行するハンズオンコードでFastAPI Backend Development Bootcampを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

FastAPI Backend Development Bootcampを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFastAPI Backend Development Bootcampは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「パスパラメーターとクエリパラメーター」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFastAPI Backend Development Bootcampレッスンでコードを書いて実行できますか?

はい。すべてのFastAPI Backend Development Bootcampレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. FastAPIの概要とセットアップ
  2. 最初のAPIエンドポイント
  3. パスパラメーターとクエリパラメーター
  4. Swagger UIによるインタラクティブなAPIドキュメント
← FastAPI Backend Development Bootcampに戻る