Query Parameters
Learn how to use query parameters in moclojer to create endpoints that respond to filters, pagination, search, and sorting.
Query parameters are values passed in the URL after the ? character, used to filter, paginate, sort, or search data. They are essential for creating flexible and dynamic RESTful APIs.
What Are Query Parameters?
Anatomy of a URL with query params:
https://api.example.com/users?role=admin&status=active&limit=10
βββ¬ββ ββββ¬ββββ ββββββ¬βββββ ββββ¬βββ
β β β β
β β β ββ limit=10
β β ββββββββββββ status=active
β βββββββββββββββββββββ role=admin
ββββββββββββββββββββββββββ Separator ?Characteristics:
Start with
?after the pathkey=valuepairs separated by&Optional (unlike path params)
Ideal for filters, pagination, search
Basic Syntax
Accessing Query Parameters
Use templates {{query-params.keyName}}:
Test:
Response:
Optional Query Parameters
All query params are optional by default:
Works with any combination:
Common Use Cases
1. Pagination
Usage:
2. Filters
Usage:
3. Search
Usage:
4. Sorting
Usage:
5. Field Selection
Usage:
Combining Multiple Query Parameters
Complete usage example:
Query Parameters with Path Parameters
Query params work perfectly with path params:
Usage:
Default Values and Missing Values
Query Param Not Provided
When a query param is not passed, the template returns an empty string:
Test without parameters:
Simulating Default Values
Use multiple endpoints with precedence:
Better alternative:
Arrays in Query Parameters
URLs can have multiple values for the same key:
In moclojer:
β οΈ Limitation: Moclojer currently returns only the last value when there are duplicates.
Workaround: Use delimiters
Special Characters
Query parameters must be URL-encoded:
Space
%20 or +
q=hello%20world
&
%26
company=A%26B
=
%3D
equation=x%3D5
#
%23
tag=%23moclojer
?
%3F
query=what%3F
Example:
Tools like curl and browsers do encoding automatically.
Complete Practical Examples
Example 1: E-commerce API
Usage:
Example 2: Blog API
Usage:
Example 3: Analytics API
Usage:
Best Practices
β
Do
Use descriptive and consistent names
Use snake_case or camelCase consistently
Boolean values as strings
Document optional parameters
β Avoid
Required parameters as query params
Sensitive data in query params
Query params too complex
Troubleshooting
Problem: Query param returns empty when should have value
Cause 1: Parameter name doesn't match
Cause 2: Spaces in name
Problem: Special character breaks URL
Solution: Use URL encoding
Problem: Number comes as string
Cause: Templates always return strings
β οΈ Caution: Without quotes, if parameter is empty, JSON becomes invalid!
Safe solution:
Next Steps
Now that you've mastered query parameters:
Path Parameters - Parameters in the URL
Body Parameters - Data in the request body
Template Variables - Complete reference
See Also
Pagination How-to - Implement pagination
Dynamic Responses Tutorial - Practical tutorial
Request Matching - How moclojer chooses endpoints
Last updated
Was this helpful?