FastAPI Training: Routing and Path Operations
Key Concepts
Routing and path operations are fundamental aspects of building APIs with FastAPI. They allow you to define how different endpoints (URL paths) should handle incoming requests. Here are the key concepts:
- Path Operations: These are the specific operations (like GET, POST, PUT, DELETE) that can be performed on a particular path.
- Path Parameters: Variables in the path that are used to capture values from the URL.
- Query Parameters: Optional parameters that are appended to the URL after a question mark (?).
- Request Body: Data sent in the body of a POST, PUT, or PATCH request.
Path Operations
Path operations define the HTTP methods (GET, POST, PUT, DELETE, etc.) that can be performed on a specific path. Each method corresponds to a different type of request:
- GET: Retrieve data from the server.
- POST: Send data to the server to create a new resource.
- PUT: Update an existing resource or create it if it doesn't exist.
- DELETE: Remove a resource from the server.
Example: Defining Path Operations
from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") def read_item(item_id: int): return {"item_id": item_id} @app.post("/items/") def create_item(item: dict): return {"item": item} @app.put("/items/{item_id}") def update_item(item_id: int, item: dict): return {"item_id": item_id, "updated_item": item} @app.delete("/items/{item_id}") def delete_item(item_id: int): return {"item_id": item_id, "status": "deleted"}
Path Parameters
Path parameters are variables in the URL path that are used to capture specific values. They are defined within curly braces {} in the path and can be accessed in the function as arguments.
Example: Using Path Parameters
from fastapi import FastAPI app = FastAPI() @app.get("/users/{user_id}") def get_user(user_id: int): return {"user_id": user_id}
Query Parameters
Query parameters are optional parameters that are appended to the URL after a question mark (?). They are typically used to filter or sort data.
Example: Using Query Parameters
from fastapi import FastAPI app = FastAPI() @app.get("/items/") def list_items(skip: int = 0, limit: int = 10): return {"skip": skip, "limit": limit}
Request Body
The request body is the data sent in the body of a POST, PUT, or PATCH request. In FastAPI, you can define the structure of the request body using Pydantic models.
Example: Using Request Body
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @app.post("/items/") def create_item(item: Item): return {"item": item}
Analogies
Think of routing and path operations as the blueprint of a house. Each room (path) has specific functions (operations) like sleeping (GET), cooking (POST), cleaning (PUT), or removing clutter (DELETE). Path parameters are like room numbers, helping you locate specific rooms. Query parameters are like filters in a library, helping you find books by genre or author. The request body is like a package you send to a friend, containing specific items.
Understanding these concepts will enable you to build robust and flexible APIs with FastAPI.