Answer
Whistle's rule syntax is very concise and intuitive, with the basic format: pattern operator-uri
Basic Rule Format
shellpattern operator-uri
- pattern: Matching pattern, can be domain, path, regular expression, etc.
- operator: Operator specifying the type of operation to perform
- uri: Target address or parameter
Common Operators
-
host: Modify request Host headershellwww.example.com host 127.0.0.1:8080 -
reqHeaders: Modify request headersshellwww.example.com reqHeaders://{custom-headers.json} -
resHeaders: Modify response headersshellwww.example.com resHeaders://{cors-headers.json} -
resBody: Modify response bodyshellwww.example.com resBody://{mock-data.json} -
resReplace: Replace response contentshellwww.example.com resReplace://old-string/new-string -
reqScript: Use script to process requestshellwww.example.com reqScript://{request-handler.js} -
resScript: Use script to process responseshellwww.example.com resScript://{response-handler.js} -
forward: Forward request to specified addressshellwww.example.com forward https://api.example.com
Matching Pattern Examples
-
Exact domain match
shellwww.example.com operator-uri -
Wildcard match
shell*.example.com operator-uri -
Path match
shellwww.example.com/api/* operator-uri -
Regular expression
shell/^https:\/\/www\.example\.com\/api\/.*/ operator-uri
Rule Priority
Whistle matches rules in the order they appear in the configuration file, from top to bottom. Once a rule matches successfully, subsequent rules are not matched. Therefore, more specific rules should be placed earlier.
Comments and Grouping
- Use
#to add comments - Use
# group-nameto create groups for easier management of large numbers of rules