Rewrite
A Rewrite rule is a named, ordered list of edits applied to every request or response that matches its pattern. Set it once and it runs on every match until you turn it off.
Adding a rule
Section titled “Adding a rule”Select Rewrite in the sidebar. A rule has a name, a pattern (the shared rule syntax), an optional method filter, and one or more actions. Every action has:
- Phase — request or response
- Target — what to touch
- Operation — what to do to it
- A name for header and query-parameter targets, a value, and for find & replace a match string
Targets and operations
Section titled “Targets and operations”| Target | Phase | Operations |
|---|---|---|
| Header | Request, response | Set · Modify if present · Remove · Find & replace |
| Body | Request, response | Replace the whole body · Empty the body · Find & replace |
| Status code | Response only | Set |
| Query parameter | Request only | Set · Modify if present · Remove · Find & replace |
| URL | Request only | Set the path · Find & replace |
- Set creates the value if it isn’t there. Modify if present only changes an existing one.
- Find & replace is a literal match by default. Turn on Treat this as a regular
expression to use a regex; capture groups are available in the replacement as
$1,$2, and so on. - Header names are matched case-insensitively. Setting a header that appears twice collapses it to one.
- A status code must be between 100 and 599.
Actions run in the order they appear. When several rules match one message, rules run in list order too.
Examples
Section titled “Examples”| Goal | Action |
|---|---|
| Add a token to every request to one API | Request · Header · Set · Authorization = Bearer … |
| Make an endpoint fail | Response · Status code · Set · 503 |
| Test a renamed JSON field | Response · Body · Find & replace · "user_name" → "username" |
| Force the web build of a site | Request · Header · Set · User-Agent = a desktop UA string |
| Drop a feature flag from the query | Request · Query parameter · Remove · beta |
Streaming and compression
Section titled “Streaming and compression”Header, status, query, and URL edits only need the message head, so responses keep
streaming while they’re applied. A body edit needs the whole body first, and HTTPGlass then
asks the server for an uncompressed response (it removes Accept-Encoding on those
requests) so a gzip’d body doesn’t silently defeat the rule. When a body is replaced, a
now-wrong Content-Encoding header is removed with it.
Order of operations
Section titled “Order of operations”Rewrite runs after same-origin Map Remote and before scripts, so a script can always override a rule.
