Rewriting HTML traditionally requires a custom middleware or a content rewriting proxy. injectHtmlTagAction collapses that into one declarative rule that handles streaming, compression, and chunked transfer correctly. It is ideal for adding scripts, banners, or stylesheets to a site without touching its source.
Use injectHtmlTagAction when you want to inject HTML, CSS, or JavaScript into a server response without modifying the upstream code. The action streams the response, decompresses common encodings on the fly, and writes the new content immediately after the first match of the chosen tag.
Typical situations include:
The action runs on the responseHeaderReceivedFromRemote scope. Supported transfer encodings include chunked. Supported content encodings include gzip, deflate, brotli, and lzw. Set restrictToHtml: true to skip non HTML responses automatically.
Adds a script to every HTML page on the target host without touching the upstream.
rules:
- filter:
typeKind: HostFilter
pattern: api.internal.example.com
actions:
- typeKind: InjectHtmlTagAction
tag: head
htmlContent: '<script src="https://localhost:8080/debug.js"></script>'
restrictToHtml: true
Makes it obvious to testers that they are looking at a non production site.
rules:
- filter:
typeKind: HostFilter
pattern: staging.example.com
actions:
- typeKind: InjectHtmlTagAction
tag: body
htmlContent: '<div style="position:fixed;top:0;left:0;background:orange;padding:4px;z-index:9999;">STAGING</div>'
restrictToHtml: true
Lets you keep larger fragments out of the YAML rules and edit them with your normal HTML tooling.
rules:
- filter:
typeKind: HtmlResponseFilter
actions:
- typeKind: InjectHtmlTagAction
tag: head
fromFile: true
fileName: /etc/fluxzy/injected.html
restrictToHtml: true
This action stream a response body and inject a text after the first specified html tag.This action can be used to inject a html code snippet after opening <head> tag in any traversing html page.This action supports chunked transfer stream and the following body encodings: gzip, deflate, brotli and lzw.
Evaluation scope defines the timing where this filter will be applied.
responseHeaderReceivedFromRemote This scope occurs the moment fluxzy has done parsing the response header.
injectHtmlTagAction
This action has no specific characteristic
The following examples apply this action to any exchanges
Inject a CSS style tag after <head> that sets the document body color to red.
rules:
- filter:
typeKind: AnyFilter
actions:
- typeKind: InjectHtmlTagAction
tag: head
htmlContent: '<style>body { background-color: red !important; }</style>'
restrictToHtml: true
Inject a file after <head>.
rules:
- filter:
typeKind: AnyFilter
actions:
- typeKind: InjectHtmlTagAction
tag: head
fromFile: true
fileName: injected.html
restrictToHtml: true
View definition of InjectHtmlTagAction for .NET integration.
The following actions are related to this action:
Not when restrictToHtml is true. The action checks the Content-Type before parsing. Leave the flag enabled unless you really want to attempt injection on any payload.
Yes. gzip, deflate, brotli, and lzw are decompressed in flight, the snippet is inserted, and the response is re emitted to the client with consistent headers.
The response passes through unchanged. The action only fires on the first occurrence of the tag.
No. Injection happens immediately after the chosen tag. Use forwardAction or a custom mock if you need finer control over the response shape.