Skip to content

Request Body Matching

Use the body field to match against the request body. Mimic supports three body formats: JSON, text, and form data.

{
"body": {
"type": "json" | "text" | "form",
/* ...mode-specific fields... */
}
}

The type field tells Mimic how to parse the body before comparing. Get this wrong and matching will silently fail — set RUST_LOG=debug if you’re not sure what’s happening.

Mimic recognizes JSON media types case-insensitively, including application/json and media types with the +json suffix.

Use for requests with Content-Type: application/json.

The entire body must match exactly:

{
"body": {
"type": "json",
"exact": {
"username": "admin",
"password": "secret"
}
}
}

Any extra fields in the request body cause the match to fail.

Only the specified fields must match — extras are fine:

{
"body": {
"type": "json",
"partial": {
"username": "admin"
}
}
}

This matches {"username": "admin"} and {"username": "admin", "remember_me": true} but not {"username": "bob"}.

{
"body": {
"type": "json",
"partial": { "username": "admin" },
"strict": true
}
}

strict: true combined with partial rejects requests that have extra fields. This is effectively the same as exact but expressed differently.

Inside a JSON partial, an expected value that is an object with a single $-prefixed key is a matcher form rather than a literal. Real payloads carry values nobody can pin in advance — generated ids, timestamps, tokens — next to the few fields that actually choose the mock:

{
"method": "POST",
"path": "/payments",
"status": 200,
"body": {
"type": "json",
"partial": {
"type": "payment",
"id": {"$any": true},
"email": {"$regex": "^[^@]+@example\\.com$"},
"amount": {"$type": "number"},
"items": {"$contains": {"sku": "A-1"}}
}
},
"response": {"accepted": true}
}
Form Meaning
{"$any": true} the field exists; any value is accepted
{"$regex": "pattern"} the field is a string matching the regex
{"$type": "number"} the value has the named JSON type: string, number, boolean, object, array, or null
{"$contains": {…shape…}} the field is an array and some element partially matches the shape

Rules:

  • An object without a lone $-key — or with more keys — compares literally, exactly as before, so existing mocks cannot change meaning.
  • A matcher form may sit at any depth, including inside a $contains shape.
  • A failing form produces a precise reason on the dashboard (/admin/requests → the request’s rejection explanations).
  • An invalid $regex is rejected per request and reported by mimic validate as a warning.
  • An unknown $-form matches nothing — a typo like "$regx" explains itself instead of silently comparing as a literal.

mocks/login_success.json:

{
"method": "POST",
"path": "/api/login",
"status": 200,
"headers": {
"required": { "content-type": { "contains": "json" } }
},
"body": {
"type": "json",
"partial": {
"username": "admin",
"password": "secret123"
}
},
"response": {
"success": true,
"token": "eyJhbGciOiJIUzI1NiIs..."
}
}

mocks/login_invalid.json:

{
"method": "POST",
"path": "/api/login",
"status": 401,
"response": {
"error": "invalid_credentials"
}
}
Terminal window
curl -X POST http://localhost:8080/api/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"secret123"}'
# 200 with token

For non-JSON text payloads:

{
"body": {
"type": "text",
"contains": "specific phrase"
}
}

Text bodies support:

Mode Example Description
exact "exact": "hello world" Body must equal the string exactly.
contains "contains": "world" Body must contain the substring.
regex "regex": "^ORDER-[0-9]+$" Body must match the regex.

For requests with Content-Type: application/x-www-form-urlencoded:

{
"body": {
"type": "form",
"fields": {
"username": "admin",
"password": "secret"
},
"strict": false
}
}

Form matching behaves like JSON partial — only the listed fields must match, and strict: true rejects extras.

Combining body matchers with other matchers

Section titled “Combining body matchers with other matchers”

You can combine body with query_params and headers for precise routing. See Match Priority for how Mimic decides when multiple body-matching mocks could apply to the same request.

  • JSON whitespace doesn’t matter. Mimic parses the JSON before comparing, so {"a":1} and { "a" : 1 } are equivalent.
  • Numbers vs strings. {"id": 1} and {"id": "1"} are different. JSON types are preserved.
  • Don’t forget the Content-Type. If your body.type is json but the request doesn’t send Content-Type: application/json, Mimic won’t try to parse it as JSON. Add a header matcher to enforce the right content type.
  • Body matching requires reading the body, so Mimic implicitly behaves as if consume_body: true when you use it. You don’t need to set the flag yourself.