Request matchers
A mock’s request object says which requests it answers. This page lists every field it accepts, as a configuration document or an AI assistant writes it, with the mock editor’s label for each. Request matching explains how MockHub picks a mock, and Match requests sets these rules in the editor.
{ "method": "POST", "urlPath": "/orders", "queryParameters": { "dryRun": { "absent": true } }, "headers": { "Content-Type": { "contains": "application/json" } }, "bodyPatterns": [{ "matchesJsonPath": { "expression": "$.currency", "equalTo": "EUR" } }]}A request matches a mock when every field on the mock holds.
Fields
Section titled “Fields”| Field | Takes | In the mock editor |
|---|---|---|
method |
One of the methods | HTTP method |
url |
The exact path and query string | Path and query |
urlPath |
The exact path, with any query string | Path only |
urlPathTemplate |
A path with named parameters | Path with parameters |
urlPathPattern |
A regular expression the whole path must match | Regex |
pathParameters |
Template parameter names, each with a comparison | Path parameters, Match a path parameter |
queryParameters |
Parameter names, each with a comparison | Query parameters, Match a query parameter |
headers |
Header names, each with a comparison | Request headers, Match a header |
cookies |
Cookie names, each with a comparison | Cookies, Match a cookie |
formParameters |
Form field names, each with a comparison | Form fields, Match a form field |
bodyPatterns |
One body comparison, or a list that must all hold | Request body, Match the body |
multipartPatterns |
A list of parts, each of which some part must match | Multipart parts, Match a multipart part |
Give a mock exactly one of url, urlPath, urlPathTemplate and urlPathPattern. pathParameters needs urlPathTemplate. url cannot be combined with queryParameters, and a mock that tries is refused with:
Cannot be combined with "queryParameters": "url" matches the path and the query string exactly. Use "urlPath" to match the path alone.A header name cannot contain . or _. Such headers never reach the gateway, as Request headers explains, so a mock that compares one is refused with:
A header name cannot contain "." or "_": the gateway drops such request headers before matching.A response rule uses the same queryParameters, headers, pathParameters, cookies, formParameters and bodyPatterns, with the same comparisons and the same refusals; it has no multipartPatterns. It chooses among the responses of the mock that matched, and never decides which mock matches.
Methods
Section titled “Methods”| Value | In the mock editor | Matches |
|---|---|---|
ANY |
Any method | Every method, including those a mock cannot name, such as HEAD |
GET |
GET | GET only |
POST |
POST | POST only |
PUT |
PUT | PUT only |
PATCH |
PATCH | PATCH only |
DELETE |
DELETE | DELETE only |
OPTIONS |
OPTIONS | OPTIONS only, such as a browser’s CORS preflight |
In a configuration document the method may be written in lower case, and a mock without method matches ANY.
A browser’s CORS preflight is an OPTIONS request with an Access-Control-Request-Method header. While the project’s Browser access (CORS) is not Off, a mock with Any method never answers one unless it compares that header itself; MockHub answers it instead. To answer preflights yourself, use OPTIONS; see Let web pages call a project.
| Field | Matches | Does not match |
|---|---|---|
"url": "/orders" |
/orders |
/orders?page=2, /orders/ |
"url": "/orders?page=2" |
/orders?page=2 |
/orders?page=2&sort=asc |
"urlPath": "/orders" |
/orders, /orders?page=2 |
/orders/, /orders/42 |
"urlPathTemplate": "/orders/{id}" |
/orders/42, /orders/42?page=2 |
/orders, /orders/, /orders/42/items |
"urlPathPattern": "/orders/[0-9]+" |
/orders/42, /orders/42?page=2 |
/orders, /orders/42/items |
url and urlPath compare text exactly, letter case and trailing slash included. urlPathTemplate and urlPathPattern compare the path alone and ignore the query string.
Path parameters
Section titled “Path parameters”A path template names the parts of a path that vary: /users/{userId}/orders/{orderId}. A {name} matches one non-empty part of the path, and a response reads what it matched as request.path.userId; see Dynamic responses.
- A template starts with
/and is at most 1,024 characters long. No part of it may be empty, so//and a trailing/are refused. - A part holds at most one
{name}, alone or beside text such as{name}.json.**is not supported; use a regular expression for a path of any depth. - A name starts with a letter or an underscore, holds letters, digits,
_and-, is at most 64 characters long and appears once. A hyphenated name is read asrequest.path.[user-id]. - Braces only wrap a name, and a literal brace cannot be written.
?and#are refused, because the query string is never part of the path. :idis not a parameter. A colon is an ordinary character in a path, so/users/:idmatches that text only.- The match is on the path as sent:
Jane%20DoestaysJane%20Doeinrequest.path.name, and{{urlEncode request.path.name decode=true}}givesJane Doe.
pathParameters compares a parameter’s value with the same comparisons as a query parameter, except absent, because a parameter always has a value:
{ "method": "GET", "urlPathTemplate": "/orders/{orderId}", "pathParameters": { "orderId": { "matches": "[0-9]+" } }}A request whose value fails a comparison does not match this mock, and another mock may answer it. A template is tried after exact paths, together with regular expressions; see When more than one mock matches.
Comparisons
Section titled “Comparisons”Each entry under queryParameters, headers, cookies, formParameters and a part’s headers is a name with one comparison: a plain string, or an object with exactly one of these keys.
| Key | In the mock editor | Holds when the value | Example |
|---|---|---|---|
equalTo |
equals | Is exactly the text, letter case included | { "equalTo": "2" } or "2" |
matches |
matches regex | Matches the regular expression as a whole | { "matches": "[0-9]+" } |
contains |
contains | Contains the text | { "contains": "json" } |
doesNotMatch |
does not match regex | Does not match the regular expression, or is missing | { "doesNotMatch": "test-.*" } |
doesNotContain |
does not contain | Does not contain the text, or is missing | { "doesNotContain": "legacy" } |
absent |
is absent | Is not in the request at all | { "absent": true } |
- A cookie is the exception to “or is missing”:
doesNotMatchanddoesNotContainhold only for a cookie the request sends. - A plain string is short for
equalTo:"Content-Type": "application/json". equalTo,matchesandcontainshold only when the parameter, header, cookie or field is present.- Header names are compared regardless of letter case. Parameter, cookie and field names and all values are compared as written.
- Some headers never reach matching, because the gateway removes or replaces them first:
Host,Accept-Encoding,Content-Lengthand the others in Gateway behaviour and limits. A comparison on one of them never sees what the caller sent.
bodyPatterns takes one comparison object, or a list of them that must all hold, at most 10. In the mock editor each is a row under Request body, added with Match the body.
| Key | In the mock editor | Holds when the body |
|---|---|---|
equalTo |
equals | Is exactly the text |
matches |
matches regex | Matches the regular expression as a whole |
contains |
contains | Contains the text |
doesNotMatch |
does not match regex | Does not match the regular expression |
doesNotContain |
does not contain | Does not contain the text |
matchesJsonPath |
has JSON path | Is JSON in which the path holds |
equalToJson |
equals JSON | Is JSON equal to the document given |
{ "bodyPatterns": [ { "matchesJsonPath": "$.items[?(@.quantity > 10)]" }, { "matchesJsonPath": { "expression": "$.customer.email", "contains": "@example.com" } }, { "doesNotContain": "\"test\":true" } ]}- The text kinds compare the body as sent, not parsed:
{"a":1}and{ "a": 1 }are different texts. For a JSON body, use the JSON kinds, which ignore spacing and key order. - A body that is not JSON matches no JSON kind. That is a miss, not an error, and another mock may answer.
absentdoes not apply to a body. WithoutbodyPatterns, any body matches.- A
bodyfield on the request object is one more exact comparison, as{ "equalTo": … }inbodyPatternsis. The mock editor shows it as an equals row and saves it intobodyPatterns.
JSON paths
Section titled “JSON paths”matchesJsonPath takes an expression, which must select something in the body, or an object with the expression and one comparison for what it selects:
| Matcher | Holds when |
|---|---|
{ "matchesJsonPath": "$.customer.id" } |
The body has customer.id, of any value but null |
{ "matchesJsonPath": "$.items[?(@.quantity > 10)]" } |
Some item has a quantity over 10 |
{ "matchesJsonPath": { "expression": "$.currency", "equalTo": "EUR" } } |
currency is the string EUR |
{ "matchesJsonPath": { "expression": "$.items[*].sku", "matches": "B-.*" } } |
Some item’s sku starts with B- |
{ "matchesJsonPath": { "expression": "$.coupon", "absent": true } } |
The body has no coupon, or it is null |
In the mock editor, choose has JSON path, enter the expression, and choose what it must do: selects something, one of the comparisons, or is absent.
What the expression may hold:
- It starts with
$, the whole document, and is at most 512 characters long. - Steps are
.name,['name with spaces'],[0],[*]and.*. An index may count from the end:[-1]is the last item. .nametakes letters, digits,_and-. Any other name is written['…'], in single quotes, and cannot hold'or\.- At most one deep scan,
..name, which finds the field at any depth. - At most two filters,
[?(…)], each comparing@or a field of it, such as@.amount, with a number, a quoted string,true,falseornull, using==,!=,<,<=,>or>=. Join comparisons with&&or||:[?(@.amount > 100 && @.currency == 'EUR')]. - Not available in a filter:
=~(use amatchescomparison instead), a filter that only tests that a field exists, such as[?(@.a)], comparisons grouped in parentheses, a field compared with another field,$,in, and a filter inside a filter. To test that a field exists, add a separate matcher whose expression is the field’s path, which must select something, or compare the field with a value. - Not available anywhere: functions such as
length(), script expressions, several names in one step such as['a','b'], and slices such as[0:2]. - A mock that uses one of these is refused with the reason, such as
A JSONPath expression cannot call a function such as length().
How a comparison reads what the expression selects:
- A string is compared as itself, and
trueandfalseas those words. - A number is compared as written in Java:
1is"1"and1.0is"1.0", so{ "equalTo": "1" }does not match1.0. Usematcheswith1(\.0)?when either can arrive. - An expression that selects several values, such as
$.items[*].sku, holds when any of them passes. - An object or array is compared as formatted JSON, which no comparison can be expected to hit. Select a field instead.
- A missing field or
nullgives no value. Onlyabsentholds then;doesNotContainanddoesNotMatchdo not. - An expression alone that selects an empty array or object still selects something. One with a filter or
[*]that finds nothing does not.
Expected JSON
Section titled “Expected JSON”equalToJson holds the expected document as a string, as a response body is written, and holds when the body is the same JSON, whatever its spacing and key order:
{ "equalToJson": "{\"currency\":\"EUR\",\"amount\":\"${json-unit.any-number}\"}", "ignoreExtraElements": true}ignoreExtraElements, Allow fields and trailing array items the JSON does not name in the editor, lets the body hold fields the document leaves out, and more array items after the ones it lists. Off, the default, the body must have exactly the document’s fields.- Array order always counts.
ignoreArrayOrderis refused, because ignoring order makes a large body take minutes to compare. - A placeholder stands for any value of a kind, written as a whole string value:
${json-unit.ignore}(anything),${json-unit.any-string},${json-unit.any-number}and${json-unit.any-boolean}. Other placeholders, such as${json-unit.regex}, are refused with a reason. To compare a field with a regular expression, usematchesJsonPathwithmatches. - The document is one JSON value of at most 64 KiB, nested at most 32 levels deep.
- A mock holds at most 10
equalToJson, across its request, its multipart parts and its rules, because each one reads the whole body again.
A body nested more than 400 levels deep matches no matchesJsonPath, and one nested more than 1,000 levels deep no equalToJson.
Cookies and form fields
Section titled “Cookies and form fields”cookies and formParameters map names to comparisons, as headers does:
{ "method": "POST", "urlPath": "/oauth/token", "cookies": { "session": { "matches": "[a-f0-9]{32}" }, "debug": { "absent": true } }, "formParameters": { "grant_type": "client_credentials", "scope": { "contains": "read" } }}- A cookie is read from the request’s
Cookieheader. Its name matches exactly, sosessionnever matchesxsession, and a quoted value is compared without its quotes. A cookie sent twice matches when either value does. absentholds for a cookie when the request has noCookieheader, an empty one, or only other cookies. A cookie that is not sent satisfies onlyabsent:doesNotMatchanddoesNotContaindo not hold for it.- A cookie name is one token: letters, digits and
!#$%&'*+-.^_`|~, with no space,=or;. - A form field is read from a body sent as
application/x-www-form-urlencoded, with or without a charset. A body of any other type, JSON or multipart included, holds no form field. - A form field that is not sent, in a request whose body is not a form too, satisfies
absent,doesNotMatchanddoesNotContain. - A form field’s name and value are compared decoded:
+is a space and%40is@. A field sent twice matches when either value does. A name holds 1 to 256 characters. - A
headerscomparison onCookiecompares the whole header’s text.
Multipart parts
Section titled “Multipart parts”multipartPatterns lists the parts a multipart/form-data or other multipart/* request must have. Each entry is matched by some part of the request; one part may satisfy several entries.
{ "method": "POST", "urlPath": "/uploads", "multipartPatterns": [ { "name": "metadata", "bodyPatterns": [{ "matchesJsonPath": "$.title" }] }, { "name": "file", "headers": { "Content-Type": { "contains": "image/" } } } ]}| Key | Takes | In the mock editor |
|---|---|---|
name |
The form field name of a multipart/form-data part |
Field name, such as file |
headers |
The part’s own header names, each with a comparison | Match a header of this part |
bodyPatterns |
The part’s body, with the same kinds as the request body | Match the body of this part |
- A mock holds at most 5 parts, and a part at most 5 matchers: its name, each header and each body comparison count one.
namematches a part whoseContent-Dispositionisform-datawithnameas its first parameter, quoted or not, as browsers and curl write it:form-data; name="file"; filename="a.png". The name is compared exactly, letter case included. A part of another multipart type is matched by itsheadersandbodyPatterns, without aname.- A part needs at least one of the three keys. A part with a
namecannot also compare itsContent-Dispositionheader, which is where the name is read from. - A part’s header names may contain
.and_: they never travel as request headers. - A request that is not multipart matches no part. A multipart request whose
Content-Typehas noboundaryis refused by the gateway before any mock is tried; see Gateway behaviour and limits.
Regular expressions
Section titled “Regular expressions”urlPathPattern, matches and doesNotMatch, a JSON path’s comparison included, take a regular expression in the usual syntax, with these rules:
| Rule | Example |
|---|---|
| The expression must match the whole value | [0-9]+ matches 42 but not order-42; .*[0-9]+ matches both |
. matches any character, line breaks included |
.*ORD-[0-9]+.* finds an order number anywhere in a body of several lines |
(?i) at the start ignores letter case |
(?i)bearer .+ matches Bearer abc and BEARER abc |
Characters with a meaning are escaped with \ |
/v1\.0/orders, written in JSON with the backslash doubled: "/v1\\.0/orders" |
{ starts a repeat count such as {2} |
/users/{id} is refused: write /users/[^/]+, or use urlPathTemplate for a named parameter |
Scenario state
Section titled “Scenario state”A mock that belongs to a scenario matches only while the scenario is in the state the mock requires. In a configuration document that condition sits beside request, not inside it:
{ "name": "GET /payments/42 after capture", "scenario": "payment", "requiredScenarioState": "captured", "request": { "method": "GET", "urlPath": "/payments/42" }, "responses": [ { "key": "settled", "name": "Settled", "response": { "statusCode": 200, "body": "{\"status\":\"settled\"}" } } ], "responseSelection": { "mode": "default", "defaultResponseKey": "settled" }}Model a stateful flow sets it up in the app.