Skip to main content
An expectation tells MockServer how to respond when an incoming request matches specific criteria. You submit expectations via PUT /mockserver/expectation with a JSON body describing the request matcher and the action to take.

PUT /mockserver/expectation

Create or update one or more expectations. Returns: 201 Created on success, 400 Bad Request if the body is invalid. You can submit a single expectation object or a JSON array of expectations. If you supply an id, MockServer updates the existing expectation with that ID or creates a new one — giving you stable, idempotent updates.

Request fields

string
Stable identifier for the expectation. Supply the same id to update an existing expectation in place. If omitted, MockServer generates a UUID.
integer
default:"0"
Match priority. When multiple expectations match a request, MockServer uses the one with the highest priority. Expectations with equal priority are evaluated in the order they were added.
object
The request matcher. MockServer evaluates all properties you provide; any property you omit is not checked. Supports matching on method, path, pathParameters, queryStringParameters, headers, cookies, body, and socketAddress. You can also match against an OpenAPI spec by providing specUrlOrPayload (and optionally operationId) instead of individual fields.
object
Return a fixed HTTP response. Use this for the majority of mocking scenarios. Mutually exclusive with httpForward, httpClassCallback, httpError, httpResponseTemplate, and httpOverrideForwardedRequest.
object
Forward the matched request to another host. Provide host, port, and scheme (HTTP or HTTPS).
object
Forward the request with modifications applied before forwarding and/or to the response before returning it.
object
Delegate response generation to a Java class on the classpath. Provide callbackClass with the fully-qualified class name.
object
Generate responses dynamically using a JavaScript or Velocity template. Provide template and templateType (JAVASCRIPT or VELOCITY).
object
Simulate a connection-level error. Set dropConnection: true to close the connection immediately, or provide responseBytes (base64-encoded) to send raw bytes before dropping.
object
Controls how many times this expectation can be matched. Omit for unlimited matching.
  • remainingTimes (integer) — match at most this many more times
  • unlimited (boolean) — if true, match indefinitely (overrides remainingTimes)
object
Controls how long the expectation remains active after it is created.
  • timeUnit (string) — e.g. "SECONDS", "MINUTES", "HOURS"
  • timeToLive (integer) — duration in the given unit
  • unlimited (boolean) — if true, the expectation never expires

Basic response example

Match GET /view/cart and return a plain-text body:

Limited-use expectation

Match a path exactly twice, then stop matching:

Expectation with TTL

Match exactly once in the next 60 seconds:

Update an expectation by ID

Supply an id to update an existing expectation or create a new one with a known identifier:

Bulk create

Submit an array of expectations in one call:

Forward expectation

Forward matching requests to another host:

Error simulation

Drop the connection for requests matching a path:

PUT /mockserver/openapi

Load expectations automatically from an OpenAPI specification. MockServer generates one expectation per operation in the spec, using the spec’s request schema as the matcher. Returns: 201 Created on success.
string
required
A URL pointing to an OpenAPI JSON or YAML file, a classpath resource path, or the raw spec content as a string.
object
Override the response code used for each operation. Keys are operationId strings; values are HTTP status code strings (e.g. "200", "404"). If omitted, MockServer picks the first defined response for each operation.

Load from a URL

Load from a URL with custom response codes

You can also match against OpenAPI operations inside a regular PUT /mockserver/expectation request by providing specUrlOrPayload (and optionally operationId) inside the httpRequest object. This lets you target a single operation while supplying a custom action.