Response Rules
Control when mock responses are returned based on request conditions. Response Rules let you create dynamic mock APIs that behave differently based on request parameters, headers, form data, and more.
What are Response Rules?
Response Rules allow you to conditionally return different mock responses based on the incoming request. You can check values from various sources (URL parameters, form data, JSON body, headers, cookies) and apply comparison logic to determine which response should be returned.
Use Cases:
- • Return different user data based on user ID parameter
- • Simulate authentication failures for specific API keys
- • Return paginated data based on page query parameters
- • Validate request headers and return appropriate errors
- • Test edge cases with specific input patterns
Rules Table
The Rules Table is where you define conditions for your mock responses. Each row represents a rule that can be enabled/disabled, combined with AND/OR operators, and reordered via drag-and-drop.
Table Columns
Drag Handle (⋮⋮)- Drag to reorder rules. Rule order affects evaluation sequence.Use- Toggle to enable/disable the rule. Disabled rules skip validation and won't be evaluated.Operator- AND/OR logic operator (not shown for the first rule)Source- Where to look for the value: Query Parameter, Form Data, JSON Body, HTTP Header, or CookieKey- The name of the parameter/field/header/cookie to check. Supports dot notation for JSON Body (e.g., user.profile.role)Comparison- Comparison operator: = Match, ≠ Not match, > Greater, < Less, >= Greater or equal, <= Less or equal, ✓ Has key, ✗ No key, ✓ Has value, ✗ No valueValue- The value to compare against. For = Match and ≠ Not match operators, you can use regex by clicking the "Regex" button or prefixing with regex:Description- Optional description to document what the rule checksActions- Plus icon adds new rows, minus icon removes rowsDefault Configuration
New responses include one pre-configured rule:
Use: OFF | Source: Query Parameter | Comparison: = Match | Key: (empty) | Value: (empty)New rows added via the + button default to disabled (Use: OFF) with Query Parameter source and = Match comparison.
Validation Rules
- When a rule is enabled (Use: ON), both Key and Value are required and cannot be empty (except for existence checks: ✓ Has key, ✗ No key, ✓ Has value, ✗ No value)
- When a rule is disabled (Use: OFF), validation is skipped and fields can be left empty
- Empty or whitespace-only values are invalid for enabled rules
- For regex patterns, the value must start with
regex:prefix or use the Regex button
Source Types
Response Rules can check values from five different sources in the incoming request:
Query Parameter
Check values from URL query parameters (?key=value format).
GET /api/users?page=1&limit=10
Rule: Query Parameter | page | = Match | 1
Rule: Query Parameter | limit | <= Less or equal | 50Form Data
Check values from POST request body form data (multipart/form-data, application/x-www-form-urlencoded).
POST /api/login
Content-Type: application/x-www-form-urlencoded
username=admin&password=secret123
Rule: Form Data | username | = Match | adminJSON Body
Check values from JSON request body (application/json). Use dot notation for nested properties.
POST /api/users
Content-Type: application/json
{
"user": {
"email": "test@example.com",
"profile": {
"role": "admin"
}
}
}
Rule: JSON Body | user.profile.role | = Match | adminHTTP Header
Check values from HTTP request headers. Header names are case-insensitive.
GET /api/data
Authorization: Bearer abc123xyz
X-API-Version: v2
Rule: HTTP Header | Authorization | regex:^Bearer\s+[A-Za-z0-9]+$
Rule: HTTP Header | X-API-Version | = Match | v2Cookie
Check values from browser cookies.
GET /api/profile
Cookie: sessionId=abc123; theme=dark
Rule: Cookie | sessionId | ✓ Has value
Rule: Cookie | theme | = Match | darkComparison Operators
Use these operators to compare values from request sources:
Value Comparison
= Match- Exact match (case-sensitive)≠ Not match- Does not matchQuery Parameter | status | = Match | active
Query Parameter | userId | ≠ Not match | guestNumeric Comparison
> Greater- Value is greater than specified< Less- Value is less than specified>= Greater or equal- Greater than or equal to<= Less or equal- Less than or equal toQuery Parameter | page | > Greater | 0
Query Parameter | limit | <= Less or equal | 100
JSON Body | user.age | >= Greater or equal | 18Existence Checks
✓ Has key- Key exists (any value, even null)✗ No key- Key does not exist✓ Has value- Key exists with non-empty value✗ No value- Key missing or has empty valueHTTP Header | Authorization | ✓ Has value
Cookie | sessionId | ✓ Has key
Query Parameter | debug | ✗ No key
JSON Body | user.email | ✓ Has valueRegex Pattern Matching
For = Match and ≠ Not match operators, you can use regular expressions by prefixing the value with regex:
Common Patterns
UUID v4
regex:^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$Matches: 550e8400-e29b-41d4-a716-446655440000
Email Address
regex:^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$Matches: user@example.com, test.user+tag@domain.co.uk
URL/Domain
regex:^https?:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(\/.*)?$Matches: https://example.com, http://api.example.com/v1/users
IPv4 Address
regex:^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$Matches: 192.168.1.1, 10.0.0.255, 8.8.8.8
Phone Number (International)
regex:^\+[1-9]\d{1,14}$Matches: +12125551234, +821012345678, +442071234567
Numeric ID
regex:^[0-9]+$Matches: 123, 456789, 1
Alphanumeric Token
regex:^[A-Za-z0-9_-]{20,}$Matches tokens with letters, numbers, underscores, hyphens (minimum 20 chars)
Starts With Pattern
regex:^test.*$Matches: test, testing, test123, test-data
User ID Pattern
regex:^user-\d+$Matches: user-123, user-456789
Multiple Choice
regex:^(active|pending|inactive)$Matches exactly: active, pending, or inactive
Usage Examples
# Ensure userId is a valid UUID
Query Parameter | userId | = Match | regex:^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
# Check if email format is valid
JSON Body | user.email | = Match | regex:^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
# Validate API key format
HTTP Header | X-API-Key | = Match | regex:^[A-Za-z0-9_-]{32,64}$Combining Rules with AND/OR
Create complex conditions by combining multiple rules with AND/OR operators.
AND Operator
All rules must match for the response to be returned.
AND [
JSON Body | user.role | = Match | admin
Cookie | sessionId | ✓ Has value
HTTP Header | Authorization | = Match | regex:^Bearer\s+[A-Za-z0-9]+$
]
→ Response returned only if all three conditions are trueOR Operator
At least one rule must match for the response to be returned.
OR [
JSON Body | user.role | = Match | admin
JSON Body | user.role | = Match | moderator
JSON Body | user.role | = Match | superuser
]
→ Response returned if role is admin, moderator, or superuserNested Logic
Combine AND/OR operators for complex conditions.
AND [
HTTP Header | Authorization | ✓ Has value
OR [
JSON Body | user.role | = Match | admin
AND [
JSON Body | user.plan | = Match | premium
JSON Body | user.verified | = Match | true
]
]
]
→ Must have auth header AND (be admin OR be premium+verified user)Real-World Examples
1. User Authentication
Return different responses based on authentication status and user role
AND [
HTTP Header | Authorization | = Match | regex:^Bearer\s+[A-Za-z0-9_-]{20,}$
JSON Body | user.email | = Match | regex:^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
JSON Body | user.password | ✓ Has value
]{
"success": true,
"user": {
"id": "{{faker 'string.uuid'}}",
"email": "{{json 'user.email'}}",
"token": "{{jwt 'HS256' '600' 'your-secret-key'}}"
}
}2. Paginated API
Return data only when page and limit parameters are valid
AND [
Query Parameter | page | > Greater | 0
Query Parameter | page | <= Less or equal | 100
Query Parameter | limit | > Greater | 0
Query Parameter | limit | <= Less or equal | 50
]{
"data": {{#repeat 10}}{
"id": "{{faker 'string.uuid'}}",
"name": "{{faker 'person.firstName'}} {{faker 'person.lastName'}}"
}{{/repeat}},
"pagination": {
"page": {{get 'page'}},
"limit": {{get 'limit'}},
"total": 1000
}
}3. API Version Control
Return different responses based on API version header
OR [
HTTP Header | X-API-Version | = Match | v2
HTTP Header | X-API-Version | = Match | 2.0
HTTP Header | Accept | = Match | regex:.*application/vnd\.api\+json;\s*version=2.*
]{
"apiVersion": "2.0",
"data": {
"type": "user",
"id": "{{faker 'string.uuid'}}",
"attributes": {
"email": "{{faker 'internet.email'}}",
"displayName": "{{faker 'person.firstName'}}"
}
}
}4. Feature Flag Testing
Enable new features for specific users or test groups
OR [
Cookie | betaTester | = Match | true
JSON Body | user.id | = Match | regex:^user-(100|101|102|103|104)$
HTTP Header | X-Feature-Flag | = Match | new-dashboard
]{
"features": {
"newDashboard": true,
"enhancedAnalytics": true
},
"message": "Welcome to the beta!"
}5. Error Simulation
Test error handling by returning errors for specific conditions
OR [
JSON Body | user.email | ≠ Not match | regex:^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
JSON Body | user.age | < Less | 18
HTTP Header | Authorization | ✗ No value
]{
"error": {
"code": "INVALID_REQUEST",
"message": "Request validation failed",
"details": [
"Invalid email format",
"User must be at least 18 years old",
"Authorization header is required"
]
}
}Tips and Best Practices
💡 Start Simple
Begin with simple single-rule conditions before building complex AND/OR logic. Test each rule individually to ensure it works as expected.
✅ Test Regex Patterns
Always test your regex patterns with multiple inputs. Use tools like regex101.com to validate patterns before adding them to rules.
⚡ Performance Considerations
Complex regex patterns and deeply nested AND/OR logic can impact performance. Keep rules as simple as possible while meeting your requirements.
🎯 Order Matters
Rules are evaluated in the order they appear. Place more specific rules before general ones to ensure the correct response is returned.
Next Steps
Explore more features of DoMock:
- → Learn about Template Syntax - Use dynamic data in your responses
- → Browse Template Examples - Get inspired by real-world use cases