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 path

  • key=value pairs 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:

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:

Character
Encoding
Example

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

  1. Use descriptive and consistent names

  2. Use snake_case or camelCase consistently

  3. Boolean values as strings

  4. Document optional parameters

❌ Avoid

  1. Required parameters as query params

  2. Sensitive data in query params

  3. 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:

  1. Path Parameters - Parameters in the URL

  2. Body Parameters - Data in the request body

  3. Template Variables - Complete reference

See Also

Last updated

Was this helpful?