Bird
Raised Fist0
Rest APIprogramming~10 mins

Query parameters for filtering in Rest API - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Query parameters for filtering
Client sends request with query parameters
↓
Server receives request
↓
Server extracts query parameters
↓
Server applies filters based on parameters
↓
Server fetches filtered data
↓
Server sends filtered data response
↓
Client receives filtered data
The client sends a request with query parameters; the server reads these parameters, filters data accordingly, and returns the filtered results.
Execution Sample
Rest API
GET /products?category=books&price_lt=20

# Server extracts 'category' and 'price_lt'
# Filters products where category='books' and price < 20
# Returns filtered product list
This example shows a client requesting products filtered by category and price using query parameters.
Execution Table
StepActionQuery Parameters ExtractedFilter AppliedResult
1Receive requestcategory=books, price_lt=20None yetWaiting to process
2Extract parameterscategory=books, price_lt=20Prepare filters: category='books', price < 20Filters ready
3Apply filterscategory=books, price_lt=20Filter products by category='books' and price < 20Filtered product list created
4Send responsecategory=books, price_lt=20Filtered data sentClient receives filtered products
5EndNo new parametersNo further filteringRequest complete
💡 All query parameters processed; filtered data sent back to client.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
query_paramsNone{category: 'books', price_lt: '20'}{category: 'books', price_lt: '20'}Used for filtering
filtersNonecategory='books', price < 20Applied filtersFilters applied
filtered_dataNoneNoneList of products matching filtersSent to client
Key Moments - 2 Insights
How does the server know which filters to apply from the query parameters?
The server extracts key-value pairs from the query parameters (see execution_table step 2) and maps them to filter conditions it understands.
What happens if a query parameter is missing or unknown?
If a parameter is missing, the server simply does not apply that filter (no restriction). Unknown parameters are ignored or cause an error depending on server design (not shown here).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what filters are prepared at step 2?
ANo filters prepared yet
Bcategory='electronics' and price > 50
Ccategory='books' and price < 20
DAll products without filtering
💡 Hint
Check the 'Filter Applied' column at step 2 in execution_table.
At which step does the server send the filtered data back to the client?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Look for 'Send response' action in the execution_table.
If the client removes 'price_lt=20' from the query, how does the filtering change?
AFilters by price only
BFilters by category only
CNo filters applied
DFilters by category and price
💡 Hint
Refer to variable_tracker for 'filters' and how query_params affect them.
Concept Snapshot
Query parameters are added to API URLs to filter data.
Server reads these parameters, applies filters to data.
Example: /items?color=red&size=small filters items by color and size.
Missing parameters mean no filter on that attribute.
Server returns only data matching all filters.
Full Transcript
When a client wants specific data from a server, it can add query parameters to the URL. These parameters are key-value pairs like 'category=books' or 'price_lt=20'. The server receives the request and extracts these parameters. Then it uses them to filter the data it holds, for example, selecting only products in the 'books' category priced less than 20. After filtering, the server sends back only the matching data. If some parameters are missing, the server does not filter by those attributes. This process helps clients get exactly the data they want without extra information.

Practice

(1/5)
1.

What is the main purpose of query parameters in a REST API URL?

easy
A. To update data on the server
B. To change the server settings
C. To filter or specify which data to return
D. To delete data from the server

Solution

  1. Step 1: Understand query parameters role

    Query parameters are added after a question mark in a URL to specify or filter data.
  2. Step 2: Identify their purpose in REST API

    They help clients ask for specific data subsets, making responses smaller and faster.
  3. Final Answer:

    To filter or specify which data to return -> Option C
  4. Quick Check:

    Query parameters = filter data [OK]
Hint: Query parameters filter data after '?' in URL [OK]
Common Mistakes:
  • Thinking query parameters change server settings
  • Confusing query parameters with request body
  • Assuming query parameters update or delete data
2.

Which of the following is the correct way to add two query parameters type=fruit and color=red to a REST API URL?

easy
A. api/items?type=fruit,color=red
B. api/items&type=fruit?color=red
C. api/items?type=fruit;color=red
D. api/items?type=fruit&color=red

Solution

  1. Step 1: Recall query parameter syntax

    Query parameters start with '?' and multiple parameters are joined with '&'.
  2. Step 2: Check each option's format

    api/items?type=fruit&color=red uses '?' then '&' correctly; others misuse separators or order.
  3. Final Answer:

    api/items?type=fruit&color=red -> Option D
  4. Quick Check:

    Use '?' then '&' between parameters [OK]
Hint: Start with '?' then join parameters with '&' [OK]
Common Mistakes:
  • Using '&' before '?'
  • Separating parameters with commas or semicolons
  • Mixing order of '?' and '&'
3.

Given the URL api/products?category=books&price_lt=20, what does the query parameter price_lt=20 most likely mean?

medium
A. Return products with price less than 20
B. Return products with price equal to 20
C. Return products with price greater than 20
D. Return products with price not equal to 20

Solution

  1. Step 1: Interpret the query parameter name

    The suffix '_lt' usually means 'less than' in filtering.
  2. Step 2: Apply meaning to the value

    price_lt=20 means filter products priced less than 20 units.
  3. Final Answer:

    Return products with price less than 20 -> Option A
  4. Quick Check:

    _lt means less than [OK]
Hint: _lt means less than, _gt means greater than [OK]
Common Mistakes:
  • Confusing _lt with equal or greater than
  • Ignoring suffix meaning in parameter names
  • Assuming price_lt filters prices equal to 20
4.

Identify the error in this URL used to filter users by age and city:
api/users?age=30&city;=NewYork

medium
A. The semicolon ';' is incorrectly used instead of '='
B. The '&' should be replaced with '?' between parameters
C. The city value should be URL encoded as 'New%20York'
D. The age parameter should be 'age_gt=30' to filter correctly

Solution

  1. Step 1: Check parameter separators and assignments

    Parameters are separated by '&' and assigned with '='. Here, 'city;=NewYork' uses ';' wrongly.
  2. Step 2: Confirm correct syntax

    The semicolon should be '=' to assign the value to 'city'.
  3. Final Answer:

    The semicolon ';' is incorrectly used instead of '=' -> Option A
  4. Quick Check:

    Use '=' to assign values, not ';' [OK]
Hint: Use '=' for assignment, '&' to separate parameters [OK]
Common Mistakes:
  • Using semicolon instead of equal sign
  • Replacing '&' with '?' between parameters
  • Not encoding spaces in values
5.

You want to filter a list of events to show only those happening in 2024 and located in either 'New York' or 'Los Angeles'. Which query parameter format correctly expresses this filter?

hard
A. api/events?year=2024&city=New York,Los Angeles
B. api/events?year=2024&city=New%20York&city=Los%20Angeles
C. api/events?year=2024&city=New York&city=Los Angeles
D. api/events?year=2024&city=New%20York|Los%20Angeles

Solution

  1. Step 1: Understand filtering multiple values for one parameter

    To filter by multiple cities, repeat the parameter with URL-encoded values.
  2. Step 2: Check URL encoding and parameter repetition

    api/events?year=2024&city=New%20York&city=Los%20Angeles repeats 'city' with URL-encoded spaces, which is standard practice.
  3. Final Answer:

    api/events?year=2024&city=New%20York&city=Los%20Angeles -> Option B
  4. Quick Check:

    Repeat parameters with URL encoding for multiple values [OK]
Hint: Repeat parameter with encoded values for multiple filters [OK]
Common Mistakes:
  • Using commas without encoding or server support
  • Not encoding spaces in city names
  • Using '|' which is not standard in URLs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard