Skip to content

Match requests

Before you begin

Role
Editor or higher.
You need
  • A mock to change, or a project to create one in

A mock answers a request only when every rule on it holds. You set the rules under Which requests it answers in the mock editor. How MockHub compares them, and why a request can match nothing, is explained in Request matching; Request matchers lists every comparison.

  1. Open the mock from the project’s Mocks tab.

  2. Choose the method in the HTTP method list, such as POST. Any method matches every method.

  3. Choose how to compare the path, as in the table below.

  4. Enter the path.

To answer Choose Enter
/orders alone, with no query string Path and query /orders
/orders?status=open, and no other query string Path and query /orders?status=open
/orders whatever its query string Path only /orders
/orders/8821, /orders/42 and any other order Path with parameters /orders/{id}
/orders/8821 and /orders/42, but not /orders/new Regex /orders/[0-9]+

With Path with parameters, each {name} stands for one part of the path, and a response can read it as request.path.name. To compare a value, open Match on more than the path, find Path parameters, select Match a path parameter, and choose the name, how to compare and the value. Typing /users/:id offers /users/{id}, because a colon is not a parameter.

A path starts with /. Path and query compares the whole query string, so it cannot be combined with query parameter rules; to match parameters one by one, choose Path only. Exact path and the query string explains the difference.

A mock with a Regex path of escaped text and [^/]+ parts, which is what imports and promoted captures wrote before Path with parameters existed, can be converted.

  1. Open the mock. Under the URL choices, a message names the path it would become, such as /orders/{id}, and counts what else changes.

  2. Select Use named parameters.

  3. Read the form. The path is now Path with parameters; every request.pathSegments.[n] in a response body or header that reads a converted part is now request.path.name; and a data store key that read such a part now names the parameter.

  4. Select Save mock.

Nothing is saved until you select Save mock; leaving the page discards the conversion. The names come from the OpenAPI operation the mock is linked to, when its parameters line up with the path, and are id, id2 and so on otherwise. Rename a parameter by editing the path and the reads that use it. A regex with anything else in it, such as [0-9]+ or .*, gets no offer.

Match headers, query parameters, cookies and form fields

Section titled “Match headers, query parameters, cookies and form fields”
  1. Open Match on more than the path.

  2. Select Match a header, Match a query parameter, Match a cookie or Match a form field.

  3. Enter the name, such as Authorization.

  4. Choose how to compare, such as matches regex.

  5. Enter the value to compare against, such as Bearer .+. is absent needs no value.

  6. Select Save mock.

To remove a rule, select its remove button, labelled for the row, such as Remove cookie matcher 1.

To answer only Rule
Requests that send JSON Header Content-Type contains application/json
Requests with a bearer token Header Authorization matches regex Bearer .+
Requests without an API key Header X-Api-Key is absent
The second page Query parameter page equals 2
Requests from a signed-in browser Cookie session matches regex .+
A client credentials token grant Form field grant_type equals client_credentials

A form field is read only from a body sent as application/x-www-form-urlencoded. A field that is not sent, in a body of any other type too, meets is absent, does not match regex and does not contain. A cookie that is not sent meets only is absent.

  1. Open Match on more than the path.

  2. Under Request body, select Match the body.

  3. Choose how to compare: one of the text comparisons, has JSON path or equals JSON.

  4. Enter what to compare against, as in the table below. For has JSON path, also choose what the path must do, such as selects something or equals, and enter the value.

  5. Add more rows the same way. The body must meet every one.

  6. Select Save mock.

To answer only Rule
Orders sent as paid has JSON path $.status, equals paid
Orders with a bulk line has JSON path $.items[?(@.quantity > 10)], selects something
Orders without a coupon has JSON path $.coupon, is absent
One exact order, whatever its layout equals JSON {"sku": "B-12", "quantity": 1}
A body naming a test account contains test-account

The text comparisons read the body exactly as the caller sent it: contains "status":"paid" does not match "status": "paid" with a space. The JSON comparisons ignore spacing and key order, and a body that is not JSON matches neither. equals JSON needs every field of the body unless Allow fields and trailing array items the JSON does not name is on. JSON paths lists what a path may hold.

  1. Open Match on more than the path.

  2. Under Multipart parts, select Match a multipart part.

  3. Enter the part’s field name in Field name, such as file.

  4. To compare the part’s own headers or body, select Match a header of this part or Match the body of this part.

  5. Add another part the same way. The request must have a part that matches each one.

  6. Select Save mock.

The mock answers only the requests that meet all its rules. Match on more than the path shows how many rules are set, even while it is closed. A request that misses a rule gets another mock’s answer, or what an unmatched request gets.

To see why a request does not match, send it with Try it: the answer lists where the request differs from what the mock expects. See Test a mock before saving.

  • The method list has GET, POST, PUT, PATCH, DELETE and OPTIONS. To answer HEAD or another method, choose Any method.
  • Path and query and query parameter rules cannot be combined. The editor refuses the pair with ““Path and query” compares the whole query string, so it cannot also match parameters one by one. Choose “Path only” to keep the parameter matchers, or remove them and type the query into the path.”
  • With Path only, a query string typed into the path and query parameter rules cannot be combined either: “Put the query string in the path or in the parameter matchers, not both — together they ask for a request that cannot exist.”
  • Path with parameters takes one {name} per part of the path and has no wildcard for a path of any depth; a part such as {name}.json is allowed. Use Regex for anything else. Path parameters lists the rules.
  • The editor checks a regular expression with your browser’s rules, and MockHub’s regular expressions differ from your browser’s in places. A pattern the editor accepts can still match nothing, so test it with Try it.
  • A few headers, such as Host and Content-Length, never reach matching, so a rule on them never sees what the caller sent. Request matchers names them.
  • MockHub does not order mocks. When two could answer the same request, make one of them more specific.
  • A request or rule holds up to 10 body rows, and a mock up to 10 equals JSON across its request, its parts and its rules. A mock holds up to 5 parts, each with up to 5 rules, its field name included.
  • has JSON path takes a limited set of expressions: no functions, =~, slices or filters within filters, and at most two filters. A filter compares a field with a value: it cannot only test that a field exists, as [?(@.a)] does, group comparisons in parentheses, compare a field with another field, read $ or use in. To test that a field exists, add a row with its path and selects something. A name with characters other than letters, digits, _ and - is written ['…']. JSON paths lists them.
  • has JSON path compares a number as Java writes it, so equals 1 does not match 1.0.

Report a problem with this page