0
0
FastAPIframework~20 mins

Trailing slash behavior in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 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.