Mockzilla logo

Routing & Matching Reference

Every request to /api/mock/{folder}/{path} goes through the same steps:

  1. Folder: find the folder from the URL.
  2. Candidates: keep the folder's enabled mocks with the same HTTP method.
  3. Filter: drop mocks whose path or required query parameters don't match.
  4. Score: pick the highest-scoring mock (see Strategy precedence).
  5. Variant: for a wildcard mock, pick a variant (see Wildcard variants).
  6. Respond: apply the delay, then proxy, or return the static, echoed, or dynamic response.

The path is relative to the folder. Trailing slashes and repeated slashes are normalized, so /users/1/ and /users//1 are both treated as /users/1. For mocks in a subfolder, the subfolder path is added in front of the mock endpoint; see Mock Subfolders.

Path matching strategies

StrategyMatches whenExample endpointMatchesDoesn't match
ExactThe path equals the endpoint/users/me/users/me/users/me/settings
WildcardThe path fits the * pattern/users/*/users/7, /users/7/posts/users
SubstringThe endpoint appears anywhere in the path/v1/v1/users, /api/v1/v2/users

Match Type is Exact by default, even when the endpoint contains *. Select Wildcard to turn on capturing and variants.


Wildcard matching

Each * captures one or more characters, including /. Captures are exposed to responses as $.params.[0], $.params.[1], … in endpoint order.

EndpointRequest pathCaptures
/api/products/*/api/products/p-42[0] = p-42
/users/*/orders/*/users/user-7/orders/order-3[0] = user-7, [1] = order-3
/users/*/orders/*/users/a/b/orders/c[0] = a/b, [1] = c
/files/*/*/files/a/b/c[0] = a, [1] = b/c
/files/*/files/2024/report.pdf[0] = 2024/report.pdf
/api/products/*/api/productsNo match

Rules:

  • Non-empty: a * never matches an empty string.
  • Shortest first: each star takes as few characters as it can while the rest still matches. Only the last star absorbs the remaining text.
  • End-anchored: a wildcard endpoint must match the end of the path, but can start partway through it. /products/* matches /products/1 and also /shop/products/1. Write the full prefix (/api/products/*) to avoid this.
  • Full URLs: an endpoint containing :// must match the whole value from start to end.
  • Literal characters: every character other than * matches itself, including ., ?, and +.

To build responses from captures, see the Dynamic Wildcard Mocks guide.


Wildcard variants

Once a wildcard mock wins the request, Mockzilla picks the response from its Wildcard Variants:

  1. A specific variant whose capture list equals the request's captures. Every position must match, and values are compared after URL-decoding.
  2. The fallback (Add fallback), if one exists. A mock has at most one.
  3. If neither exists, Return 404 when no variant matches decides:
    • On: 404 with the error No matching variant found.
    • Off: the mock's stored base response.

If the mock has no variants at all, the base response is used.

The chosen variant replaces the response body, status code, body type, JSON Schema, and dynamic mode. Echo Request Body is always off for variant responses. Delay and proxy settings stay on the mock and still apply.

Variants are chosen after mock selection. They don't affect which mock wins.

EndpointVariant capturesRequestResult
/products/*p-404/products/p-404Specific variant
/products/*p-404/products/p-42Fallback, or 404/base
/users/*/orders/*user-7, order-3/users/user-7/orders/order-3Specific variant
/users/*/orders/*user-7, order-3/users/user-7/orders/order-9Fallback, or 404/base

Matching by query parameters

Use Advanced Options → Query Parameters to require query values before a mock can match. Add one row per required key.

Required rowRequest queryResult
(none)?page=2, or no queryMatches
category = books?category=books&page=2Matches; extra keys are allowed
category = books?category=gamesDoesn't match
category = *?category=gamesMatches; any value
category = *?category=Matches; an empty value counts
category = *?page=2Doesn't match; the key is missing
category = boo*?category=booksMatches values starting with boo
role = adm*?role=superadminDoesn't match; the pattern starts at the beginning

Rules:

  • Every row must match. Requests can include extra parameters.
  • Key names match exactly and are case-sensitive.
  • A value without * must equal the request value. * matches any characters, including none, and the pattern covers the whole value.
  • A repeated key (?tag=a&tag=b) uses its last value.

You can also type query parameters in the Endpoint Path field, such as /users?role=admin. The editor moves them into Advanced Options and keeps both places in sync. The saved endpoint never contains ?.

Query rows only control matching. To return a value, reference it in the response with {{$.query.key}}. Values are strings; see Use request values in responses. Query values don't change schema generation. For example, ?limit=10 doesn't change an array's length; set minItems and maxItems for that.


Strategy precedence

Each mock that passes the path and query filters gets a score. The highest score wins.

score = path type + 10 × required query rows + exact query values + endpoint length
PartPoints
Path typeExact 300, Wildcard 200, Substring 100
Each required query row+10
Each query row with an exact value (no *)+1
Endpoint path length+1 per character, query string excluded

Worked examples

Request /users/admin:

MockScore
Exact /users/admin300 + 12 = 312 ✅
Wildcard /users/*200 + 8 = 208
Substring /users100 + 6 = 106

Request /users/1?role=admin:

MockScore
Wildcard /users/*, role = admin200 + 10 + 1 + 8 = 219 ✅
Wildcard /users/*, role = *200 + 10 + 8 = 218
Wildcard /users/*200 + 8 = 208

Request /users/1/profile?role=admin:

MockScore
Wildcard /users/*, role = admin200 + 11 + 8 = 219 ✅
Wildcard /users/*/profile200 + 16 = 216

The last example shows that a query row (+10) usually outweighs a longer path. Path type is worth the most, but it can be overtaken: an exact mock beats a wildcard with the same query rows, while a wildcard with about ten extra query rows can outscore an exact path.

Ties

When scores are equal:

  1. Mocks without a proxy target win over proxy mocks.
  2. The most recently created mock wins.

Avoid relying on ties. Give overlapping mocks distinct endpoints or query rows, or disable the one you don't want.


Proxy & Record

When the matched mock has meta.proxyTargetUrl:

  1. It proxies the request to that URL, keeping the query string. Each * in the target is replaced by the matching wildcard capture.
  2. It records the response as a new exact mock in the same folder and subfolder.
  3. The recording answers only the exact path and query it captured; a request with a different or extra query still reaches the proxy.

If no mock matches, Mockzilla returns 404. Folder settings never turn on proxy recording by themselves.

See the Proxy & Record Guide for more.


FAQ

I have /users/admin (Exact) and /users/* (Wildcard). Which one answers /users/admin? The exact mock (312 vs 208). It stays the winner unless the wildcard mock has about ten more required query rows.

How do I return a different response for one ID without a second mock? Add a specific variant for that capture value on the wildcard mock.

Why does my mock return 404 when the path is right? Check the HTTP method, that the mock is enabled, and every required query row. A key = * row still requires the key to be present.

Why does /products/* answer /shop/products/1? Wildcards are anchored to the end of the path, not the start. Use the full prefix.