0Pricing
FastAPI Backend Development Bootcamp · บทเรียน

พารามิเตอร์เส้นทางและคำค้น

ทำความเข้าใจวิธีดึงข้อมูลแบบไดนามิกจาก URL ด้วยพารามิเตอร์เส้นทาง และประมวลผลพารามิเตอร์คำค้นที่ไม่บังคับ

พารามิเตอร์เส้นทางและคำค้น เป็นบทเรียน FastAPI Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.

คำถามที่พบบ่อย

บทเรียน “พารามิเตอร์เส้นทางและคำค้น” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “พารามิเตอร์เส้นทางและคำค้น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส FastAPI Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “พารามิเตอร์เส้นทางและคำค้น”

ทำความเข้าใจวิธีดึงข้อมูลแบบไดนามิกจาก URL ด้วยพารามิเตอร์เส้นทาง และประมวลผลพารามิเตอร์คำค้นที่ไม่บังคับ คุณปฏิบัติ FastAPI Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน FastAPI Backend Development Bootcamp หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน FastAPI Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “พารามิเตอร์เส้นทางและคำค้น” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน FastAPI Backend Development Bootcamp นี้ได้ไหม

ได้ บทเรียน FastAPI Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รู้จัก FastAPI และการตั้งค่า
  2. จุดปลายทาง API แรกของคุณ
  3. พารามิเตอร์เส้นทางและคำค้น
  4. เอกสาร API แบบโต้ตอบด้วย Swagger UI
← กลับไปที่ FastAPI Backend Development Bootcamp