docs/en/docs/rules/resBody.md
The resBody protocol is used to replace the response body of a specified request. It only takes effect on status codes that include a response body (such as 200, 500, etc.).
Effective Condition: Only takes effect on status codes with a response body.
⚠️ Note: Requests without a response body, such as
204,304, are not affected.
resBody supports multiple ways to specify the response content to replace:
Write the content to replace directly in the rule, suitable for short text content.
pattern resBody://(value) [lineProps...] [filters...]
Example:
www.example.com/api/data resBody://({"status":"modified"})
This method is recommended when dealing with complex content containing spaces, line breaks, or when you want to reuse a configuration.
pattern resBody://{custom-key.json} [lineProps...] [filters...]
``` custom-key.json
{
"status": "success",
"data": {
"message": "This response was modified by resBody"
}
}
```
When the content is large, you can store it in the Values configuration area.
pattern resBody://{key-of-values.html} [lineProps...] [filters...]
Prerequisite: A key named key-of-values.html with the content to replace as its value must exist in Values.
Load the response content to replace from a local file or remote URL.
# Load from a local file
pattern resBody:///User/xxx/test.txt
# Load from a remote URL
pattern resBody://https://www.example.com/override-content.txt
Use Whistle's temporary file feature when content needs frequent editing.
pattern resBody://temp/blank.txt
Steps:
Command (Mac) / Ctrl (Windows).resBody://temp/blank.txt.Save to save.| Parameter | Required | Description & Examples |
|---|---|---|
| pattern | Yes | Expression used to match the request URL. |
| • Supports domains, paths, wildcards, regular expressions. | ||
| • See Matching Pattern Documentation for details. | ||
| value | Yes | The response content to replace, supporting multiple formats: |
| • Local file path | ||
| • Remote URL | ||
| • Inline, embedded, Values references | ||
| lineProps | No | Sets additional properties for the rule. |
• Example: lineProps://important can increase this rule's priority. | ||
| • See lineProps Documentation for details. | ||
| filters | No | Optional filter conditions for precise control over when the rule takes effect. |
| • Can match request URL, method, headers, body content. | ||
| • Can match response status code, headers. | ||
| • See Filters Documentation for details. |
# Replace API response content
www.example.com/api/data resBody://({"status":"custom","data":"modifiedContent"})
``` error-response
{
"error": {
"code": 1001,
"message": "Custom error message for testing"
}
}
```
www.example.com/api resBody://{error-response} includeFilter://s:500
# Load replacement content from a local file
www.example.com/api/config resBody:///Users/username/mock/config.json
# Load replacement content from a remote URL
www.example.com/api/data resBody://https://raw.githubusercontent.com/user/repo/main/mock-data.json
www.example.com/api/user resBody://temp/blank.json
In the Rules editor, hold down
Command(Mac) /Ctrl(Windows) and click onresBody://temp/blank.jsonwith the mouse to edit.
# Replace only responses with a specific status code
www.example.com/api resBody://({"error":"ServiceUnavailable"}) includeFilter://s:503
# Determine replacement content based on request method
www.example.com/api/users resBody://({"method":"GEToverride"}) includeFilter://m:GET
www.example.com/api/users resBody://({"method":"POSToverride"}) includeFilter://m:POST
Use regular expressions for dynamic content construction:
# Replace the timestamp in the original response with the current time
www.example.com/api/time resBody://`({"timestamp":${now}})`
# Development environment: Use mock data
dev-api.example.com resBody://{"env":"development","data":"mock"}
# Simulate various error responses
www.example.com/api resBody://{"error":"RateLimitExceeded"} includeFilter://s:429
www.example.com/api resBody://{"error":"InternalServerError"} includeFilter://s:500
www.example.com/api resBody://{"error":"ServiceUnavailable"} includeFilter://s:503
The main distinction between the resBody protocol and the file protocol lies in the request processing flow:
resBody protocol: The request is first sent to the backend server to obtain the original response, then replaces the original response body with the specified content.file protocol: The request is not sent to the backend server; it directly returns the specified file content.resBody: Suitable for modifying the return content of real APIs while maintaining the complete request-response flow.file: Suitable for complete local simulation, independent of real backend services.resBody replaces the response body content and does not automatically modify the Content-Type response header.resType protocol.A: Check:
A: Yes, use filters:
# Replace only JSON responses
pattern resBody://content includeFilter://resH:content-type=json
# Replace only responses from specific paths
pattern resBody://content includeFilter://*/api/v1/
Content-Type.Prepend To Body): resPrependPrepend HTML To Body, response type must be text/html): htmlPrependPrepend CSS To Body, response type must be text/html or text/css): cssPrependPrepend JS To Body, response type must be text/html, text/css, or application/javascript): jsPrependReplace Body): resBodyReplace Body, response type must be text/html): htmlBodyReplace Body, response type must be text/html or text/css): cssBodyReplace Body, response type must be text/html, text/css, or application/javascript): jsBodyAppend To Body): resAppendAppend HTML To Body, response type must be text/html): htmlAppendAppend CSS To Body, response type must be text/html or text/css): cssAppendAppend JS To Body, response type must be text/html, text/css, or application/javascript): jsAppendModify Body Text): resReplaceModify Form/JSON): resMergeDelete Form/JSON): delete://resBody.xxx