📖 Scenario: You are building a simple web API using FastAPI. You want to understand how FastAPI handles URLs with and without trailing slashes.
🎯 Goal: Create two API endpoints: one with a trailing slash and one without. Observe how FastAPI responds to requests to these endpoints with and without trailing slashes.
📋 What You'll Learn
Create a FastAPI app instance named app
Create a GET endpoint at /items/ (with trailing slash) that returns a JSON message
Create a GET endpoint at /users (without trailing slash) that returns a JSON message
Use the exact function names read_items and read_users for the endpoints
💡 Why This Matters
🌍 Real World
Web APIs often have URLs with or without trailing slashes. Understanding how your framework handles these helps avoid broken links or unexpected redirects.
💼 Career
Backend developers must know how to define routes correctly and handle URL patterns to build reliable APIs.
Progress0 / 4 steps
1
Create FastAPI app instance
Import FastAPI from fastapi and create an app instance called app.
FastAPI
Hint
Use app = FastAPI() to create the app instance.
2
Add GET endpoint with trailing slash
Add a GET endpoint at /items/ (with trailing slash) using @app.get("/items/"). Define a function called read_items that returns {"message": "Items endpoint with slash"}.
FastAPI
Hint
Use @app.get("/items/") decorator and define read_items function.
3
Add GET endpoint without trailing slash
Add a GET endpoint at /users (without trailing slash) using @app.get("/users"). Define a function called read_users that returns {"message": "Users endpoint without slash"}.
FastAPI
Hint
Use @app.get("/users") decorator and define read_users function.
4
Add root endpoint to test trailing slash behavior
Add a GET endpoint at / using @app.get("/"). Define a function called read_root that returns {"message": "Root endpoint"}. This helps test how FastAPI handles root URL with or without slash.
FastAPI
Hint
Use @app.get("/") decorator and define read_root function.
Practice
(1/5)
1. In FastAPI, what happens if you define a route with a trailing slash like @app.get('/items/') and a client requests /items without the slash?
easy
A. FastAPI will redirect the client to /items/ automatically.
B. FastAPI will return a 404 Not Found error.
C. FastAPI will treat both URLs as the same without any redirect.
But when you request /orders/ with a trailing slash, you get a 404 error. How can you fix this to accept both /orders and /orders/ without errors?
medium
A. Add a middleware to ignore trailing slashes
B. Change the route to @app.get('/orders/') only
C. Use a wildcard route like @app.get('/orders*')
D. Define a second route with trailing slash: @app.get('/orders/')
Solution
Step 1: Understand FastAPI's strict matching
FastAPI treats /orders and /orders/ as different routes and does not match one to the other automatically unless redirect applies.
Step 2: Add explicit route for trailing slash
Defining @app.get('/orders/') alongside @app.get('/orders') allows both URLs to be handled without 404.
Final Answer:
Define a second route with trailing slash: @app.get('/orders/') -> Option D
Quick Check:
Explicit routes for both slash and no-slash = A [OK]
Hint: Add both slash and no-slash routes to avoid 404 [OK]
Common Mistakes:
Assuming one route handles both URLs automatically
Using wildcard incorrectly
Expecting middleware to fix trailing slash issues by default
5. You want a FastAPI route to accept both /profile and /profile/ URLs without redirect or 404 errors, and return the same response. Which approach is best?
hard
A. Define one route with trailing slash and rely on FastAPI redirect.
B. Define one route without trailing slash and rely on FastAPI redirect.
C. Define two routes: @app.get('/profile') and @app.get('/profile/') with the same function.
D. Use a custom middleware to strip trailing slashes before routing.
Solution
Step 1: Understand FastAPI's redirect behavior
FastAPI redirects missing trailing slash URLs to the defined route with trailing slash, but this causes a redirect, not direct handling.
Step 2: Avoid redirects by defining both routes
Defining both @app.get('/profile') and @app.get('/profile/') with the same function handles both URLs directly without redirect or 404.
Final Answer:
Define two routes: @app.get('/profile') and @app.get('/profile/') with the same function. -> Option C
Quick Check:
Two explicit routes avoid redirect and 404 = B [OK]
Hint: Define both slash and no-slash routes for no redirect [OK]
Common Mistakes:
Relying on redirect when direct handling is needed
Using middleware unnecessarily
Defining only one route expecting both URLs to work
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.