StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 2 · Query Parameters

Repeated And Aliased Parameters

easy100%1 of 1 solved

Problem statement

The item listing handler declares no query parameters, so every request fails. Declare tag so that every occurrence of it is collected into a list, and read the text filter from the hyphenated name item-query, with both falling back to their defaults when they are left out.

Requirements

The app, the GET / home route, the item data in app/store.py, the find_items search in app/services/library.py and the body of the list_items handler are already provided. The body answers with tags and query as it used them, then the results that find_items selected. Your task has one part, in app/api/routes/items.py: declare the two query parameters of list_items, named tag and item_query.

A query parameter typed as a list collects every occurrence of its name in the query string, in the order they arrive. A list has to be marked as a query parameter, otherwise FastAPI expects it in the request body. A parameter can also be given an alias, a public name that differs from the name in the handler. This is how a name with a hyphen, which Python names cannot hold, is read, and once a parameter has an alias, only the alias is read.

  • tag is optional text that can be repeated. Every occurrence is collected into a list in the order it arrived, with repeats kept, so tag=design arrives as ["design"] and tag=urgent&tag=design&tag=urgent arrives as ["urgent", "design", "urgent"]. A request without tag uses the list ["featured"]
  • item_query is optional text that defaults to the empty string
  • item_query is read from the public name item-query only. item-query=guide arrives as guide, and a value sent as item_query, with an underscore, is ignored
Sample Requests & Responses

GET /items

Response (200 OK):

{"tags": ["featured"], "query": "", "results": [{"id": 1, "title": "Atlas Onboarding Guide", "owner": "mira", "tags": ["featured", "draft"]}, {"id": 3, "title": "Canvas Style Guide", "owner": "mira", "tags": ["design", "featured"]}, {"id": 9, "title": "Indigo Sprint Board", "owner": "mira", "tags": ["featured", "draft"]}, {"id": 12, "title": "Lantern Service Map", "owner": "mira", "tags": ["backend", "featured"]}]}

GET /items?tag=urgent&tag=archive

Response (200 OK):

{"tags": ["urgent", "archive"], "query": "", "results": [{"id": 4, "title": "Delta Query Plan", "owner": "priya", "tags": ["backend", "urgent"]}, {"id": 8, "title": "Harbor Query Log", "owner": "omar", "tags": ["urgent", "design"]}, {"id": 14, "title": "Nectar Query Guide", "owner": "omar", "tags": ["urgent", "backend"]}, {"id": 7, "title": "Granite Brand Kit", "owner": "priya", "tags": ["design", "archive"]}, {"id": 10, "title": "Juniper Archive Index", "owner": "priya", "tags": ["archive", "design"]}]}

GET /items?tag=design&item-query=guide

Response (200 OK):

{"tags": ["design"], "query": "guide", "results": [{"id": 3, "title": "Canvas Style Guide", "owner": "mira", "tags": ["design", "featured"]}, {"id": 11, "title": "Kestrel Token Guide", "owner": "omar", "tags": ["design", "draft"]}]}

GET /items?tag=backend&item_query=query

Response (200 OK):

{"tags": ["backend"], "query": "", "results": [{"id": 2, "title": "Beacon Release Notes", "owner": "omar", "tags": ["backend"]}, {"id": 4, "title": "Delta Query Plan", "owner": "priya", "tags": ["backend", "urgent"]}, {"id": 6, "title": "Foxglove Query Cheatsheet", "owner": "mira", "tags": ["backend", "draft"]}, {"id": 12, "title": "Lantern Service Map", "owner": "mira", "tags": ["backend", "featured"]}, {"id": 14, "title": "Nectar Query Guide", "owner": "omar", "tags": ["urgent", "backend"]}]}

More in Query Parameters

See the module
Optional Search FiltersThis problem covers searching a store's product catalog with filters a caller may leave out, so an omitted filter falls back to a sensible default and a value that makes no sense is refused with a clear reason.100%1 of 1 solvedeasyConstrained Query RulesThis problem covers checking the search values a caller sends before they are used, so a term that is too short or a page size that is too large is refused rather than obeyed.100%1 of 1 solvedeasyCatalog Search ContractThis problem covers accepting a full set of search options from a caller, filling in the ones left out, refusing the ones that break a rule, and reporting back exactly which options the search ran with.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.