乐闻世界logo
搜索文章和话题

What is the rule syntax of whistle and what are the common operators?

2月21日 16:25

Answer

Whistle's rule syntax is very concise and intuitive, with the basic format: pattern operator-uri

Basic Rule Format

shell
pattern 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

  1. host: Modify request Host header

    shell
    www.example.com host 127.0.0.1:8080
  2. reqHeaders: Modify request headers

    shell
    www.example.com reqHeaders://{custom-headers.json}
  3. resHeaders: Modify response headers

    shell
    www.example.com resHeaders://{cors-headers.json}
  4. resBody: Modify response body

    shell
    www.example.com resBody://{mock-data.json}
  5. resReplace: Replace response content

    shell
    www.example.com resReplace://old-string/new-string
  6. reqScript: Use script to process request

    shell
    www.example.com reqScript://{request-handler.js}
  7. resScript: Use script to process response

    shell
    www.example.com resScript://{response-handler.js}
  8. forward: Forward request to specified address

    shell
    www.example.com forward https://api.example.com

Matching Pattern Examples

  1. Exact domain match

    shell
    www.example.com operator-uri
  2. Wildcard match

    shell
    *.example.com operator-uri
  3. Path match

    shell
    www.example.com/api/* operator-uri
  4. 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-name to create groups for easier management of large numbers of rules
标签:Whistle