response-rewrite
Rewrites the response status, headers and body.
response-rewritePriority 899What it does
response-rewrite changes the answer the client gets from the route: its status under Status code, its headers under Headers, and its body. Body replaces the whole body, and Filters replace what a pattern matches inside it.
Body and Filters never go together in one configuration. A route that needs a fixed body takes the first, a route that corrects a few words of the backend's answer takes the second.
When to use it
- Every answer of the orders API must tell browsers and proxies not to keep a copy, with
Cache-Controlset tono-store. - A header the backend adds about itself must not leave the gateway, removed under Remove.
- A backend that names an internal host in its answers, where clients must see the public name instead, rewritten under Filters.
- A route kept for old clients that answers with a fixed status and body while the backend behind it is retired.
Not for changing the request the backend receives, its path, Host or headers — see proxy-rewrite (Rewrites the request URI, method, host and headers before proxying) or answering with a made-up response and no backend at all — see mocking (Answers requests with a mocked response instead of an upstream).
Guides
Pages that walk through a job with response-rewrite, step by step.
- Set headers on every answerPut response-rewrite on a route to set, add or remove headers on every answer its clients get.
Set it up
Hide the gateway's Server header and forbid caching of every response
Choose Add, set and remove and fill Headers as in the JSON below.
{
"headers": {
"set": {
"Cache-Control": "no-store"
},
"remove": [
"Server"
]
}
}
What you will see
- Every answer carries
Cache-Control: no-store, whatever the backend sent. - No
Serverheader is among the answer's headers: the gateway removed the one it would have added.
Things to know
- Set — Set writes the header on the answer: with
Cache-Controlatno-store, a request to the route is answered200carryingCache-Controlwithno-store. - Add — An entry without a colon,
noColonHerefor example, is refused when you save. - Body — A configuration that carries both Body and Filters is refused when you save.
- Body base64 — On, a Body that is not Base64,
not base64!!for example, is refused when you save. - Status code — A code from
200to598is taken;999is refused when you save. - Response body limit — Past this limit, the rest of what has arrived is dropped, and whatever the upstream sends after it passes unfiltered.
- AdviceTry a pattern under Filters against a real answer of the backend before you save it on a busy route.
Settings
The fields as the console shows them, section by section.
status_codeintegerThe status code the response is sent with.
bodystringReplaces the response body with this text. Not stored beside Filters.
body_base64booleandefault falseDecodes the body from Base64 before it is sent, for binary content.
Choose one:
One value per header
headersobjectHeaders added to, set on or removed from the response. An object whose keys are a name without a colon, each with a string or number as its value.
Add, set and remove
headersobjectHeaders added to, set on or removed from the response.
headers.addarray of stringsOne header per entry, written as Name: value, with no second colon.
headers.removearray of stringsheaders.setobjectAn object whose keys are a name without a colon, each with a string or number as its value.
filtersarray of objectsReplacements made in the response body, each by a pattern and its substitute. Not stored beside Body.
filters[].regexstringRequiredThe pattern matched in the response body.
filters[].replacestringRequiredThe text put in place of each match; it may refer to captured groups.
filters[].optionsstringdefault joThe regex options, such as case-insensitive matching.
filters[].scopestringdefault onceWhether the first match or every match in the body is replaced.
varsarray[["status","==",200]]max_resp_body_sizeintegerdefault 67108864Bytes of the response the filters read, 64 MiB unless set. Past this, the rest of what has arrived is dropped, and whatever the upstream sends after it passes unfiltered.