Request Body Matching
Use the body field to match against the request body. Mimic supports three body formats: JSON, text, and form data.
Choosing a body type
Section titled “Choosing a body type”{ "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.
JSON body matching
Section titled “JSON body matching”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.
Exact match
Section titled “Exact match”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.
Partial match (recommended default)
Section titled “Partial match (recommended default)”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"}.
Strict partial
Section titled “Strict partial”{ "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.
JSON matcher forms
Section titled “JSON matcher forms”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
$containsshape. - A failing form produces a precise reason on the dashboard
(
/admin/requests→ the request’s rejection explanations). - An invalid
$regexis rejected per request and reported bymimic validateas a warning. - An unknown
$-form matches nothing — a typo like"$regx"explains itself instead of silently comparing as a literal.
Worked example: login endpoint
Section titled “Worked example: login endpoint”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" }}curl -X POST http://localhost:8080/api/login \ -H "Content-Type: application/json" \ -d '{"username":"admin","password":"secret123"}'# 200 with tokencurl -X POST http://localhost:8080/api/login \ -H "Content-Type: application/json" \ -d '{"username":"admin","password":"wrong"}'# 401 invalid_credentials (falls through to the second mock)Text body matching
Section titled “Text body matching”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. |
Form body matching
Section titled “Form body matching”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.
Common gotchas
Section titled “Common gotchas”- 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.typeisjsonbut the request doesn’t sendContent-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: truewhen you use it. You don’t need to set the flag yourself.