0
0
FastAPIframework~10 mins

Trailing slash behavior in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a FastAPI route that responds to GET requests at '/items/'.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items[1]")
def read_items():
    return {"message": "Items list"}
Drag options to blanks, or click blank then click option'
A/
C//
Ditems
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the trailing slash when the route expects it causes a 404 error.
Using double slashes '//' is invalid syntax for route paths.
2fill in blank
medium

Complete the code to define a FastAPI route that responds to GET requests at '/users' without a trailing slash.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/users[1]")
def read_users():
    return {"message": "Users list"}
Drag options to blanks, or click blank then click option'
A/
C//
Dusers
Attempts:
3 left
💡 Hint
Common Mistakes
Adding a trailing slash when the route is defined without one causes 404 errors.
Using double slashes '//' is invalid and causes errors.
3fill in blank
hard

Fix the error in the route decorator to correctly handle requests to '/products/'.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/products[1]")
def read_products():
    return {"message": "Products list"}
Drag options to blanks, or click blank then click option'
B/
Cproducts
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving out the trailing slash causes 404 errors when the URL ends with a slash.
Using double slashes '//' is invalid.
4fill in blank
hard

Complete the code to create a route that matches '/orders' without a trailing slash and returns a JSON message.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/orders")
def read_orders():
    return {"message": "Orders[1]"}
Drag options to blanks, or click blank then click option'
B/
C list
Ds
Attempts:
3 left
💡 Hint
Common Mistakes
Adding a slash in BLANK_1 causes route mismatch.
Not adding descriptive text in the message makes output unclear.
5fill in blank
hard

Fill all three blanks to define a route that matches '/customers/' with trailing slash and returns a JSON with key 'name' and value 'Customer'.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/customers[1]")
def read_customers():
    return {{"[2]": "[3]"}}
Drag options to blanks, or click blank then click option'
A/
Bname
CCustomer
Dcustomer
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the trailing slash causes route mismatch.
Using wrong keys or values in the returned JSON.