Complete the code to require the query parameter 'age' to be greater than 18.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(age: int = Query(..., gt=[1])): return {"age": age}
ge instead of gt changes the condition to 'greater or equal'.The gt parameter in Query sets the minimum value that the query parameter must be greater than. Here, it is set to 18.
Complete the code to require the query parameter 'price' to be less than 100.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/products/") async def get_products(price: float = Query(..., lt=[1])): return {"price": price}
le instead of lt allows price equal to 100.The lt parameter means 'less than'. Setting lt=100 means the price must be below 100.
Fix the error in the code to require 'quantity' to be greater or equal to 1.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/orders/") async def get_orders(quantity: int = Query(..., [1]=1)): return {"quantity": quantity}
gt excludes 1 from valid values.le or lt sets upper limits instead.The ge parameter means 'greater or equal'. Setting ge=1 allows quantity 1 or more.
Fill both blanks to require 'rating' to be between 1 and 5 inclusive.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/reviews/") async def get_reviews(rating: int = Query(..., [1]=1, [2]=5)): return {"rating": rating}
gt or lt excludes boundary values.Use ge=1 for minimum value 1 and le=5 for maximum value 5, both inclusive.
Fill all three blanks to require 'score' to be greater than 0, less than or equal to 100, and default to 50.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/scores/") async def get_scores(score: int = Query([1], gt=[2], [3]=100)): return {"score": score}
lt instead of le excludes 100.The default value is set to 50. The score must be greater than 0 (gt=0) and less than or equal to 100 (le=100).