Challenge - 5 Problems
Trailing Slash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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:
What happens when you request
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"}
Attempts:
2 left
💡 Hint
Think about how FastAPI handles routes with trailing slashes and requests without them.
✗ Incorrect
FastAPI automatically redirects requests missing a trailing slash to the route with trailing slash using a 307 Temporary Redirect status.
❓ component_behavior
intermediate2:00remaining
What happens when you define a FastAPI route without trailing slash and request it with trailing slash?
Given this route:
What is the response when requesting
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": []}
Attempts:
2 left
💡 Hint
Check how FastAPI treats trailing slashes when the route is defined without one.
✗ Incorrect
FastAPI does not redirect from a URL with trailing slash to one without. It treats them as different routes, so /users/ returns 404 if only /users is defined.
📝 Syntax
advanced2: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.
Attempts:
2 left
💡 Hint
Check the route string for invalid characters or patterns.
✗ Incorrect
Double slashes in route paths like "/path//" are invalid and cause a syntax error in FastAPI route definitions.
❓ state_output
advanced2: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:
If you request
@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?Attempts:
2 left
💡 Hint
FastAPI adds or removes trailing slashes to match route definitions.
✗ Incorrect
FastAPI redirects to the route with trailing slash if the route is defined with trailing slash, so the final URL is /products/.
🔧 Debug
expert3:00remaining
Why does this FastAPI app return 404 when requesting /api/items/ even though route is defined as /api/items?
Code snippet:
Requesting
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]}
Attempts:
2 left
💡 Hint
Think about how FastAPI handles trailing slashes when the route is defined without one.
✗ Incorrect
FastAPI does not redirect from a URL with trailing slash to one without. They are treated as different routes, so /api/items/ returns 404 if only /api/items is defined.