Bird
Raised Fist0
FastAPIframework~20 mins

Trailing slash behavior in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Trailing Slash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the response status when accessing a FastAPI route without trailing slash if the route is defined with trailing slash?
Consider this FastAPI route:
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_items():
    return {"message": "Hello"}

What happens when you request /items (without trailing slash)?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_items():
    return {"message": "Hello"}
AFastAPI responds with a 307 redirect to /items/ (adds trailing slash)
BFastAPI responds with 200 OK and returns the JSON directly
CFastAPI returns 404 Not Found error
DFastAPI responds with 500 Internal Server Error
Attempts:
2 left
💡 Hint
Think about how FastAPI handles routes with trailing slashes and requests without them.
❓ component_behavior
intermediate
2:00remaining
What happens when you define a FastAPI route without trailing slash and request it with trailing slash?
Given this route:
from fastapi import FastAPI
app = FastAPI()

@app.get("/users")
async def read_users():
    return {"users": []}

What is the response when requesting /users/ (with trailing slash)?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/users")
async def read_users():
    return {"users": []}
AFastAPI responds with 500 Internal Server Error
BFastAPI responds with 307 redirect to /users (removes trailing slash)
CFastAPI responds with 404 Not Found error
DFastAPI responds with 200 OK and returns the JSON
Attempts:
2 left
💡 Hint
Check how FastAPI treats trailing slashes when the route is defined without one.
📝 Syntax
advanced
2:00remaining
Which route definition causes a syntax error in FastAPI?
Select the option that will cause a syntax error when defining a FastAPI route related to trailing slashes.
A
@app.get("/path")
async def func():
    return {}
B
@app.get("/path//")
async def func():
    return {}
C
@app.get("/path/{id}/")
async def func(id: int):
    return {"id": id}
D
@app.get("/path/")
async def func():
    return {}
Attempts:
2 left
💡 Hint
Check the route string for invalid characters or patterns.
❓ state_output
advanced
2:00remaining
What is the final URL path after FastAPI redirects when requesting a route without trailing slash if route has trailing slash?
Given this route:
@app.get("/products/")
async def get_products():
    return {"products": []}

If you request /products, what URL will the browser end up at after FastAPI's redirect?
A/products/
B/products//
C/products
D/products/index
Attempts:
2 left
💡 Hint
FastAPI adds or removes trailing slashes to match route definitions.
🔧 Debug
expert
3:00remaining
Why does this FastAPI app return 404 when requesting /api/items/ even though route is defined as /api/items?
Code snippet:
from fastapi import FastAPI
app = FastAPI()

@app.get("/api/items")
async def items():
    return {"items": [1,2,3]}

Requesting /api/items/ returns 404. Why?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/api/items")
async def items():
    return {"items": [1,2,3]}
AThe server is misconfigured and blocks trailing slash URLs
BThe route handler has a syntax error causing 404
CFastAPI automatically removes trailing slashes, so /api/items/ is invalid
DFastAPI treats /api/items and /api/items/ as different routes and does not redirect from /api/items/ to /api/items
Attempts:
2 left
💡 Hint
Think about how FastAPI handles trailing slashes when the route is defined without one.

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.
D. FastAPI will raise a server error.

Solution

  1. Step 1: Understand FastAPI's default trailing slash behavior

    FastAPI automatically redirects requests without a trailing slash to the route with a trailing slash if defined that way.
  2. Step 2: Apply this to the given route

    Since the route is defined with a trailing slash, a request to /items will redirect to /items/.
  3. Final Answer:

    FastAPI will redirect the client to /items/ automatically. -> Option A
  4. Quick Check:

    Trailing slash route causes redirect = C [OK]
Hint: Trailing slash routes redirect missing slash URLs [OK]
Common Mistakes:
  • Thinking FastAPI treats slash and no-slash URLs identically without redirect
  • Assuming 404 error on missing slash
  • Believing server error occurs
2. Which of the following is the correct way to define a FastAPI route that matches exactly /users without a trailing slash?
easy
A. @app.get('/users/')
B. @app.get('/users')
C. @app.get('/users/*')
D. @app.get('/users?')

Solution

  1. Step 1: Recall route syntax for exact path matching

    To match a path without trailing slash exactly, define the route without the slash.
  2. Step 2: Check each option

    @app.get('/users') uses @app.get('/users') which matches /users exactly without trailing slash.
  3. Final Answer:

    @app.get('/users') -> Option B
  4. Quick Check:

    Exact no-slash route = D [OK]
Hint: No trailing slash in route means no slash in URL [OK]
Common Mistakes:
  • Adding trailing slash when none is wanted
  • Using wildcard or query syntax incorrectly
  • Confusing optional trailing slash with exact match
3. Given this FastAPI code snippet:
@app.get('/products')
async def get_products():
    return {'message': 'No slash'}

@app.get('/products/')
async def get_products_slash():
    return {'message': 'With slash'}
What will be the JSON response when a client requests /products/?
medium
A. {'message': 'With slash'}
B. {'message': 'No slash'}
C. 404 Not Found error
D. Redirect to /products without slash

Solution

  1. Step 1: Identify route matching for /products/

    The route @app.get('/products/') matches the URL /products/ exactly.
  2. Step 2: Determine which function runs

    The function get_products_slash handles /products/ and returns {'message': 'With slash'}.
  3. Final Answer:

    {'message': 'With slash'} -> Option A
  4. Quick Check:

    Trailing slash URL matches trailing slash route = A [OK]
Hint: Trailing slash URL matches trailing slash route exactly [OK]
Common Mistakes:
  • Assuming no slash route handles slash URL
  • Expecting redirect instead of direct match
  • Confusing 404 error with route mismatch
4. You have this FastAPI route:
@app.get('/orders')
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

  1. 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.
  2. Step 2: Add explicit route for trailing slash

    Defining @app.get('/orders/') alongside @app.get('/orders') allows both URLs to be handled without 404.
  3. Final Answer:

    Define a second route with trailing slash: @app.get('/orders/') -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Define two routes: @app.get('/profile') and @app.get('/profile/') with the same function. -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard