Query parameters help you ask for only the data you want from a list. They make responses smaller and faster.
Query parameters for filtering in Rest API
/resource?key1=value1&key2=value2
Use ? to start query parameters after the main URL.
Use & to add more filters.
/books?author=Jane%20Austen/users?status=active&age=30/products?price_lt=50This small web app shows how to filter books by author using query parameters. If you visit /books?author=Jane%20Austen, it returns only books by Jane Austen.
from flask import Flask, request, jsonify app = Flask(__name__) # Sample data books = [ {"id": 1, "title": "Pride and Prejudice", "author": "Jane Austen"}, {"id": 2, "title": "1984", "author": "George Orwell"}, {"id": 3, "title": "Emma", "author": "Jane Austen"} ] @app.route('/books') def get_books(): author = request.args.get('author') if author: filtered = [book for book in books if book['author'].lower() == author.lower()] else: filtered = books return jsonify(filtered) if __name__ == '__main__': app.run(debug=True)
Query parameters are case-sensitive in URLs but you can handle case in your code.
Always encode special characters in query parameters (like spaces become %20).
Filtering on the server side helps reduce data sent over the internet.
Query parameters let you ask for specific data from a big list.
They start with ? and use & to add more filters.
Use them to make your app faster and easier to use.
Practice
What is the main purpose of query parameters in a REST API URL?
Solution
Step 1: Understand query parameters role
Query parameters are added after a question mark in a URL to specify or filter data.Step 2: Identify their purpose in REST API
They help clients ask for specific data subsets, making responses smaller and faster.Final Answer:
To filter or specify which data to return -> Option CQuick Check:
Query parameters = filter data [OK]
- Thinking query parameters change server settings
- Confusing query parameters with request body
- Assuming query parameters update or delete data
Which of the following is the correct way to add two query parameters type=fruit and color=red to a REST API URL?
Solution
Step 1: Recall query parameter syntax
Query parameters start with '?' and multiple parameters are joined with '&'.Step 2: Check each option's format
api/items?type=fruit&color=red uses '?' then '&' correctly; others misuse separators or order.Final Answer:
api/items?type=fruit&color=red -> Option DQuick Check:
Use '?' then '&' between parameters [OK]
- Using '&' before '?'
- Separating parameters with commas or semicolons
- Mixing order of '?' and '&'
Given the URL api/products?category=books&price_lt=20, what does the query parameter price_lt=20 most likely mean?
Solution
Step 1: Interpret the query parameter name
The suffix '_lt' usually means 'less than' in filtering.Step 2: Apply meaning to the value
price_lt=20 means filter products priced less than 20 units.Final Answer:
Return products with price less than 20 -> Option AQuick Check:
_lt means less than [OK]
- Confusing _lt with equal or greater than
- Ignoring suffix meaning in parameter names
- Assuming price_lt filters prices equal to 20
Identify the error in this URL used to filter users by age and city:api/users?age=30&city;=NewYork
Solution
Step 1: Check parameter separators and assignments
Parameters are separated by '&' and assigned with '='. Here, 'city;=NewYork' uses ';' wrongly.Step 2: Confirm correct syntax
The semicolon should be '=' to assign the value to 'city'.Final Answer:
The semicolon ';' is incorrectly used instead of '=' -> Option AQuick Check:
Use '=' to assign values, not ';' [OK]
- Using semicolon instead of equal sign
- Replacing '&' with '?' between parameters
- Not encoding spaces in values
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?
Solution
Step 1: Understand filtering multiple values for one parameter
To filter by multiple cities, repeat the parameter with URL-encoded values.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.Final Answer:
api/events?year=2024&city=New%20York&city=Los%20Angeles -> Option BQuick Check:
Repeat parameters with URL encoding for multiple values [OK]
- Using commas without encoding or server support
- Not encoding spaces in city names
- Using '|' which is not standard in URLs
Ready to prove it?
Pass the test and this pattern moves unknown
