Set headers on every answer
The answers of the orders API should all carry some headers and lose others, whatever the backend sends. response-rewrite changes the answer on its way back to the client. This guide sets Cache-Control to no-store on every answer of orders-api, so that browsers and proxies keep no copy, and removes the Server header. At the end, your backend stays as it is, and the client gets its answers with the headers you chose.
Before you start
- This guide starts from
orders-apias Route requests to a backend leaves it, with no other plugin. A plugin from another guide that this page does not name can change the answers below; remove it first or work on a copy of the route. - The requests below call
/orders/42. - The host name of your gateway. In the console it is the first part of the Address line on a route's Overview tab,
api.example.comin this page's examples; replace it in every request below. - The headers to set and to remove, and the value of each header you set.
- The Editor or Environment administrator role in this environment.
Steps
1. Choose response-rewrite
Open orders-api under Routes, select its Plugins tab, then Add plugin at the top right of the tab. Open the Transformation category, or type response-rewrite in the search box. Select response-rewrite, then Configure.
2. Choose the three lists
Under Headers, choose Add, set and remove. It shows three lists: headers to append, headers to write over and headers to drop.
3. Set one header and remove another
Under Set, select Add Set. The new row has two boxes, the header's name and its value. Under Remove, type a value and press Enter for each.
| Field | Value | Why |
|---|---|---|
| Set | Cache-Control in the first box, no-store in the second | Every answer carries it, whatever the backend sent. |
| Remove | Server | The gateway adds this header to answers itself. Listed here, it is gone from the answer. |
Leave Add empty, and leave the fields for the status and the body as they are. An empty field leaves that part of the answer as the backend sent it.
4. Add the plugin
Select Add plugin at the bottom of the panel. The tab now lists response-rewrite, and the plugin is saved like this:
{
"headers": {
"set": {
"Cache-Control": "no-store"
},
"remove": [
"Server"
]
}
}
Check it
Send a request to the route and read the headers of the answer.
curl -i -L 'https://<gateway host>/orders/42'
Among the response headers your curl prints, Cache-Control: no-store is there, and no Server header is.
- The caching header
- Every answer carries
Cache-Control: no-store, whatever the backend sent.
Set the same headers on every route
A plugin on a route changes the answers of that route only. To change the answers of every route of the environment, put response-rewrite with the same settings on a global rule. Its plugins run on every route in the environment. Apply a plugin to every request creates the rule and adds plugins to it.
What to know
- The other choice under Headers, One value per header, writes every header with the value given there, replacing what the answer carries.
- Add is a list like Remove, not two boxes like Set. Each entry is one value, the header's name, a colon and its value, with no second colon. An entry without the colon is refused when you save.
- response-rewrite changes what the client gets, not what the backend receives. To change the request on its way to the backend, see Rewrite the path your backend sees.
- The plugin can also replace the status and the body of the answer. The reference lists those fields.
Related
- Apply a plugin to every request: the same plugin on a global rule, for every route at once.
- Let a browser app call the API: cors on a route, so a web app served from another origin can call the API from the browser.
- Answer with a mock while the backend is built: an answer made up by the gateway, with no backend behind it.
- response-rewrite in the reference.