0
0
FastAPIframework~20 mins

Required query parameters in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a required query parameter is missing?

Consider this FastAPI endpoint:

from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_item(q: str):
    return {"q": q}

What is the response if the client calls /items/ without the q parameter?

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_item(q: str):
    return {"q": q}
AThe server returns a 500 Internal Server Error.
BThe server returns an empty string for 'q' in the JSON response.
CThe server returns a 404 Not Found error.
DThe server returns a 422 Unprocessable Entity error because the required query parameter 'q' is missing.
Attempts:
2 left
💡 Hint

Think about what FastAPI does when a required parameter is not provided.

📝 Syntax
intermediate
2:00remaining
Which code correctly defines a required query parameter with a default value?

Which of the following FastAPI endpoint definitions correctly makes q a required query parameter?

Aasync def read_item(q: str):
Basync def read_item(q: str = None):
Casync def read_item(q: str = ""):
Dasync def read_item(q: Optional[str] = None):
Attempts:
2 left
💡 Hint

Required parameters do not have default values.

state_output
advanced
2:00remaining
What is the output when a required query parameter is provided?

Given this FastAPI endpoint:

from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search(q: str):
    return {"query": q.upper()}

What is the JSON response when the client calls /search?q=fastapi?

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search(q: str):
    return {"query": q.upper()}
A{"query": "FASTAPI"}
B{"query": "fastapi"}
C422 Unprocessable Entity error
D{"query": null}
Attempts:
2 left
💡 Hint

Look at how the parameter q is used inside the function.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint raise a validation error?

Examine this code:

from fastapi import FastAPI
app = FastAPI()

@app.get("/items")
async def get_items(q: str = None):
    return {"q": q.upper()}

When calling /items without query parameters, what error occurs and why?

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items")
async def get_items(q: str = None):
    return {"q": q.upper()}
A422 Unprocessable Entity error because q is required but missing
BAttributeError because q is None and None has no method upper()
CNo error, returns {"q": ""}
DTypeError because q is not a string
Attempts:
2 left
💡 Hint

Consider what happens when q is None and you call upper().

🧠 Conceptual
expert
2:00remaining
How to enforce a required query parameter with a default value in FastAPI?

In FastAPI, you want a query parameter q that is required but also has a default value if not provided. Which statement is true?

AUse <code>q: str</code> with a validator to assign a default value.
BUse <code>q: str = "default"</code> to make it required with a default.
CYou cannot have a required query parameter with a default value; providing a default makes it optional.
DUse <code>q: str = Query(..., default="default")</code> to make it required with a default.
Attempts:
2 left
💡 Hint

Think about what 'required' means in FastAPI query parameters.