Recall & Review
beginner
What is a query parameter in FastAPI?
A query parameter is a value sent in the URL after a question mark (?) that the server can read and use. In FastAPI, you declare it as a function parameter in your path operation function.
Click to reveal answer
beginner
How do you declare a basic query parameter in FastAPI?
You add a function parameter with a default value or type annotation in your path operation function. For example: <br>
def read_item(q: str = None): declares a query parameter named 'q'.Click to reveal answer
beginner
What happens if you don't provide a default value for a query parameter in FastAPI?
FastAPI treats it as a required query parameter. The client must send it in the URL, or FastAPI will return an error.
Click to reveal answer
beginner
Example: What does this FastAPI function do?<br>
@app.get('/items/')<br>def read_items(q: str = None):<br> return {'q': q}It declares a GET endpoint '/items/' that accepts an optional query parameter 'q'. If 'q' is sent in the URL, it returns it in a dictionary. If not sent, it returns {'q': None}.
Click to reveal answer
beginner
Why use query parameters instead of path parameters?
Query parameters are good for optional or filtering data without changing the URL structure. Path parameters are for required parts of the URL that identify a resource.
Click to reveal answer
How do you declare an optional query parameter named 'search' in FastAPI?
✗ Incorrect
Declaring with a default value None makes the query parameter optional.
What happens if a required query parameter is missing in the request?
✗ Incorrect
FastAPI expects required query parameters and returns an error if missing.
Which symbol starts query parameters in a URL?
✗ Incorrect
Query parameters start after a question mark (?) in the URL.
In FastAPI, how do you access the value of a query parameter inside your function?
✗ Incorrect
FastAPI matches query parameters to function parameters by name.
Which of these is a valid query parameter declaration in FastAPI?
✗ Incorrect
Declaring with a default value and type is valid and makes the parameter optional.
Explain how to declare and use a basic query parameter in FastAPI.
Think about how the function parameter relates to the URL query string.
You got /4 concepts.
Describe the difference between required and optional query parameters in FastAPI.
Consider what happens if the client does not send the parameter.
You got /4 concepts.