Consider a REST API endpoint that supports a search parameter to filter users by age greater than 30.
Given the following request URL:
GET /users?age_gt=30
And the server code snippet that filters users:
users = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 35},
{"name": "Charlie", "age": 40}
]
filtered = [user for user in users if user["age"] > int(request.args.get("age_gt", 0))]
print(filtered)What will be printed?
users = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 35},
{"name": "Charlie", "age": 40}
]
class Request:
def __init__(self):
self.args = {"age_gt": "30"}
request = Request()
filtered = [user for user in users if user["age"] > int(request.args.get("age_gt", 0))]
print(filtered)Think about which users have age greater than 30.
The code filters users whose age is greater than 30. Alice is 25, so excluded. Bob and Charlie are 35 and 40, so included.
When you want to retrieve data filtered by search parameters in a REST API, which HTTP method should you use?
Think about which method is used to get data without changing it.
GET is used to retrieve data and can include search parameters in the URL query string.
Look at this code snippet that tries to filter products by a minimum price from a search parameter:
products = [
{"name": "Pen", "price": 1.5},
{"name": "Notebook", "price": 3.0}
]
min_price = request.args.get("min_price")
filtered = [p for p in products if p["price"] >= min_price]
print(filtered)What error will this code raise when the request URL is /products?min_price=2?
products = [
{"name": "Pen", "price": 1.5},
{"name": "Notebook", "price": 3.0}
]
class Request:
def __init__(self):
self.args = {"min_price": "2"}
request = Request()
min_price = request.args.get("min_price")
filtered = [p for p in products if p["price"] >= min_price]
print(filtered)Check the types of p["price"] and min_price.
The min_price is a string from the request arguments. Comparing float >= string causes a TypeError.
In a Flask app, you want to get the 'category' search parameter from the URL query string. Which code snippet is correct?
Remember the syntax for getting query parameters in Flask.
request.args.get('category', default='all') correctly gets the parameter with a default value.
Given this list of books and a search parameter 'year' filtering books published after that year:
books = [
{"title": "Book A", "year": 1999},
{"title": "Book B", "year": 2005},
{"title": "Book C", "year": 2010},
{"title": "Book D", "year": 2015}
]
search_year = int(request.args.get("year", "2000"))
filtered_books = [b for b in books if b["year"] > search_year]
print(len(filtered_books))If the request URL is /books?year=2005, how many books will be printed?
books = [
{"title": "Book A", "year": 1999},
{"title": "Book B", "year": 2005},
{"title": "Book C", "year": 2010},
{"title": "Book D", "year": 2015}
]
class Request:
def __init__(self):
self.args = {"year": "2005"}
request = Request()
search_year = int(request.args.get("year", "2000"))
filtered_books = [b for b in books if b["year"] > search_year]
print(len(filtered_books))Count books with year greater than 2005.
Books C (2010) and D (2015) have year > 2005, so 2 books.