add_interception_rule
Modify HTTP traffic on the fly by adding interception rules: inject headers, replace request/response bodies, or block URLs. Proxy must be running.
Instructions
Add a rule that modifies HTTP traffic in real-time as it passes through the mitmproxy.
PREREQUISITE: Proxy must be running (call manage_proxy(action="start") first).
NOTE: These rules apply to ALL traffic through the proxy (browser + code-mode + replay). Rules are cached for ~5 seconds, so changes are not instant. For instant browser-only interception, use browser_intercept_request/response instead.
Available actions:
"inject_header": Add/override a header. Requires key and value. Example: add_interception_rule(url_pattern=".api.", action="inject_header", key="X-Admin", value="true")
"replace_body": Find and replace text in the body. Requires search_pattern and value. Example: add_interception_rule(url_pattern=".*", action="replace_body", search_pattern="false", value="true", resource_type="response")
"block": Block matching requests entirely. Example: add_interception_rule(url_pattern=".analytics.", action="block")
Args: url_pattern: Regex pattern to match URLs (e.g. ".api.target.com.", ".*.js$") action: "inject_header", "replace_body", or "block" resource_type: "request" or "response" (default: "request") key: Header name (required for inject_header only) value: Header value (for inject_header) or replacement text (for replace_body) search_pattern: Regex to find in body (required for replace_body only) method: Only match this HTTP method, e.g. "POST" (optional, matches all methods if omitted)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | No | ||
| value | No | ||
| action | Yes | ||
| method | No | ||
| url_pattern | Yes | ||
| resource_type | No | request | |
| search_pattern | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |