Template variables
Use {{request.path}}, {{response.status}} and other template variables to reuse parts of a request or response inside a Mittelware rule.
Reuse parts of the traffic inside a rule's action. Wrap a variable in double curly braces and Mittelware fills it in when the rule runs.
Think of it as a mail-merge for requests. If you write a rule that redirects every call to your laptop, you don't want to type each path by hand. {{request.path}} stands in for "whatever path this request used", and Mittelware swaps in the real one every time. New here? Build a plain rule first with the Rules walkthrough, then come back.
http://localhost:3000{{request.path}}
Template variables work in the action fields of a rule (URLs, header values, query values and bodies). When you save, Mittelware rejects variables it doesn't recognise or that aren't available for the chosen action, so a typo can't silently produce an empty value.
Let autocomplete do the typing#
You don't have to remember the names. In any action field, type {{ and a list of every variable available there pops up. This example is a Modify Request rule, in its URL field, after typing http://localhost:3000{{:

Keep typing and the list narrows to what matches. After request.p, only three entries remain:

Pick one with the arrow keys and Enter (or click it). Mittelware completes the closing }} for you and colours the finished variable blue, so you can see at a glance which parts are variables and which are plain text:

In the body editor#
Response bodies are written in a small code editor, which has the same autocomplete in a slightly different style: a scrollable list, with the closing }} added the moment you type {{.

In a Modify Response rule you also get the response.* variables, which don't exist in a Modify Request rule because there's no response yet. Type response. to jump straight to them:

The conditions.* variables show up when the rule's conditions can provide them (see below).
What each action offers#
| Where you are | Variables suggested |
|---|---|
| Modify Request (any field) | request.* |
| Modify Response (including the text of a Stream body) | request.* and response.* |
| Proxy to different endpoint | request.* and response.* |
| Modify Response, Instead of the server (see Answer without the server) | request.*. There is no response to read |
| Finally… rows (see Finally) | request.* and response.* |
| WebSocket rules | request.* and message, see Message variables |
| Every field above | vars.*, see Your variables |
Entries that end in a placeholder, such as request.headers.Header-Name, are patterns: replace the placeholder with the real name, for example request.headers.User-Agent.
Request variables#
Available in every action.
| Variable | Value |
|---|---|
{{request.url}} |
The full URL |
{{request.scheme}} |
http or https |
{{request.domain}} |
The host name |
{{request.port}} |
The port |
{{request.path}} |
The path, without the query string |
{{request.query_string}} |
The whole query string as sent, without the leading ?. Empty if there is none |
{{request.method}} |
The HTTP method |
{{request.body}} |
The request body |
{{request.headers.Name}} |
The value of the request header Name |
{{request.params.name}} |
The value of the query parameter name |
Message variables#
Available in a WebSocket Modify Message action only: the new message, Find and Replace with. The request variables above work there too, and read the request the connection was opened with. There are no response variables, because a WebSocket has no response.
| Variable | Value |
|---|---|
{{message}} |
The text of the message being changed |
{{message.direction}} |
sent or received |
{{message.type}} |
text, binary, ... |
{{message.size}} |
The size in bytes |
Response variables#
Available once a response exists: in Modify Response and Proxy to different endpoint. Modify Request runs before any response, so it can only use the request variables.
| Variable | Value |
|---|---|
{{response.status}} |
The status code |
{{response.body}} |
The response body |
{{response.headers.Name}} |
The value of the response header Name |
Values from the rule's own conditions#
A rule can also read what its own conditions matched, numbered from zero in the order they appear.
{{conditions.0.value}}: the value of the first condition.{{conditions.0.matches.1.value}}: capture group 1 of the first condition, when it uses the matches regex operator. Group0is the whole match.{{conditions.0.values.0.value}}: the first entry of the list in the first condition, when it uses is one of or is none of.
These are only offered, and only accepted, for conditions whose operator supports them.
Your variables#
{{vars.name}} reads one of the variables you keep on the Variables page: a token, a tenant, a host. They are offered after {{vars. and work in every field above, in every kind of rule.
Authorization: Bearer {{vars.token}}
https://{{vars.tenant}}.example.com{{request.path}}
- A variable is read when the rule runs, so changing it on the Variables page applies to the next request without saving the rule.
- Names aren't case sensitive:
{{vars.Token}}and{{vars.token}}read the same variable. - A variable that doesn't exist reads as nothing. The rule form doesn't flag a misspelt name, so check it if a value comes out empty.
- The value of a variable is data, not a template. A
{{request.method}}inside a value is sent exactly as typed. {{vars.}}or{{vars.a b}}, which can't be a variable's name, are left alone as plain text.
A rule can also set a variable from the traffic it sees: see Finally.
Examples#
Send staging traffic to your laptop#
Rule: URL starts with https://staging.acme.dev → Modify Request.
URL: http://localhost:3000{{request.path}}
X-Original-Host: {{request.domain}}
{{request.path}} doesn't include the query string. To keep it all, add {{request.query_string}} after a ?, for example http://localhost:3000{{request.path}}?{{request.query_string}}. To keep only some of the parameters, name them, for example http://localhost:3000{{request.path}}?page={{request.params.page}}.
Echo a header back in a mocked response#
Rule: URL contains /v1/echo → Modify Response.
{ "you_sent": "{{request.headers.User-Agent}}", "method": "{{request.method}}" }
Carry a captured ID into a mock#
Rule: URL matches regex /v1/orders/(\d+) → Modify Response.
{ "id": {{conditions.0.matches.1.value}}, "status": "mocked" }
Syntax notes#
- Tags can't be nested. The first
}}after a{{closes it. - Spaces just inside the braces are ignored:
{{ request.path }}works too.
Back to the Rules page.