Dynamic Responses

Master dynamic API responses with template variables. Use path params, query strings, JSON body, and headers to create realistic mocks. Learn {{path-params}}, {{query-params}}, and more.

In the previous tutorial, you created static endpoints that always return the same response. Now you'll learn how to make your responses dynamic - changing based on the data sent in the request. This makes your mocks much more realistic and useful for testing.

What you'll learn

  • How to use path parameters in URLs

  • How to capture and use query parameters

  • How to access JSON data from request bodies

  • How to use template variables to create dynamic content

  • How to test dynamic responses

What you'll build

An enhanced user API that responds dynamically to different inputs:

  • GET /users/:id - Returns user data based on the ID in the URL

  • GET /users - Filters users based on query parameters

  • POST /users - Creates users with data from the request body

  • GET /search - Search functionality with dynamic results

Prerequisites

  • Completed the Your First Mock Server tutorial

  • moclojer running on your system

  • Basic understanding of HTTP requests

Step 1: Understanding template variables

Template variables are placeholders in your responses that get replaced with actual data from the request. They use the {{variable}} syntax.

Available template variables:

  • {{path-params.name}} - Values from URL path (e.g., :id, :username)

  • {{query-params.name}} - Values from query string (e.g., ?search=value)

  • {{json-params.name}} - Values from JSON request body

  • {{header-params.name}} - Values from HTTP headers

📚 Deep dive: For complete details on each parameter type, see our dedicated guides:

Step 2: Path parameters

Path parameters let you capture parts of the URL and use them in your response.

Create a new moclojer.yml file:

Test it:

Start your server and try these requests:

Notice how the responses change based on the values in the URL!

Step 3: Query parameters

Query parameters come after the ? in URLs and let you pass additional data.

Add this endpoint to your configuration:

Test it:

Step 4: JSON body parameters

For POST, PUT, and PATCH requests, you can access data from the JSON request body.

Add these endpoints:

Test it:

Step 5: Header parameters

You can also access HTTP headers in your responses:

Test it:

Step 6: Combining multiple parameters

Real APIs often use multiple types of parameters. Here's a comprehensive example:

Test it:

Step 7: Handling missing parameters

moclojer gracefully handles missing parameters by showing them as empty strings:

Understanding data types

When using template variables, remember:

  • Strings need quotes: "name": "{{json-params.name}}"

  • Numbers don't need quotes: "age": {{json-params.age}}

  • Booleans don't need quotes: "active": {{json-params.active}}

Example with mixed types:

Your complete dynamic configuration

Here's your complete moclojer.yml with all the dynamic examples:

What you've accomplished

Path parameters - Capture values from URLs ✅ Query parameters - Use URL query strings ✅ JSON body parameters - Access request body data ✅ Header parameters - Read HTTP headers ✅ Combined parameters - Use multiple parameter types together ✅ Data type handling - Work with strings, numbers, and booleans

Next steps

Your mock APIs are now much more realistic and flexible! In the next tutorial, you'll learn how to organize multiple endpoints and create a complete API with proper structure.

👉 Multiple Endpoints Tutorial - Learn to organize and structure larger APIs

Need help?

See Also

Last updated

Was this helpful?