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
Stable identifier for the expectation. Supply the same
id to update an existing expectation in place. If omitted, MockServer generates a UUID.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.
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.Return a fixed HTTP response. Use this for the majority of mocking scenarios. Mutually exclusive with
httpForward, httpClassCallback, httpError, httpResponseTemplate, and httpOverrideForwardedRequest.Forward the matched request to another host. Provide
host, port, and scheme (HTTP or HTTPS).Forward the request with modifications applied before forwarding and/or to the response before returning it.
Delegate response generation to a Java class on the classpath. Provide
callbackClass with the fully-qualified class name.Generate responses dynamically using a JavaScript or Velocity template. Provide
template and templateType (JAVASCRIPT or VELOCITY).Simulate a connection-level error. Set
dropConnection: true to close the connection immediately, or provide responseBytes (base64-encoded) to send raw bytes before dropping.Controls how many times this expectation can be matched. Omit for unlimited matching.
remainingTimes(integer) — match at most this many more timesunlimited(boolean) — iftrue, match indefinitely (overridesremainingTimes)
Controls how long the expectation remains active after it is created.
timeUnit(string) — e.g."SECONDS","MINUTES","HOURS"timeToLive(integer) — duration in the given unitunlimited(boolean) — iftrue, the expectation never expires
Basic response example
MatchGET /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 anid 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.
A URL pointing to an OpenAPI JSON or YAML file, a classpath resource path, or the raw spec content as a string.
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.