Document updated on Jul 30, 2024
Response header transformation
The response headers modifier allows you to add, replace, or delete headers declaratively before responses are returned to the end-user for all declared endpoints simultaneously.
The main benefit of the modifier/response-headers is that the execution happens at the end of the response flow, and you can transform headers from all types of endpoints or encodings or change headers set by KrakenD itself (like the X-KrakenD-Complete and similar)
With this component, you can transform the returning headers no matter how they were generated.
Configuration of response headers transformation
The configuration of the modifier is quite simple, and is declared at the service level, acting over all endpoints’ activity. If you want to transform headers in a single endpoint, see the different ways below.
mY-HEAdEr becomes My-Header.Here is a configuration example that performs the three types of transformations:
- Adds an
X-Hello: Worldheader to all responses - Replaces any existing value of
Cache-Controlto set ano-storevalue - Deletes the
Serverheader set by the backend
{
"version": 3,
"extra_config": {
"modifier/response-headers": {
"add": {
"X-Hello": [
"World"
]
},
"replace": {
"Cache-Control": [
"no-store"
],
"Vary": [
"foo",
"bar",
"foobar"
]
},
"delete": [
"Server"
],
"rename": {
"X-Krakend-Completed": "X-Completed"
}
}
}
}
The operations are always executed in this fixed order:
deletereplaceaddrename
Fields of Response headers modifier
addobject- The headers you want to add. Every key under
addis the header name, and the values are declared in an array with all those you want to set. If the header didn’t exist previously, it is created with the values you passed. If the header existed, then the new values are appended.Example:{"X-Hello":["World"]}Properties of
addcan use a name matching the pattern(.+), and their content is anarray deletearray- The list of headers you want to delete. All headers listed will be missing in the response.Example:
["X-Krakend","X-Krakend-Completed"] renameobject- The headers you want to rename. The key used under
renameis the original header name, and the value the new header name. This operation is destructive, meaning that if you rename to a header name that already existed it will be replaced with the new header and value.Example:{"Header-A":"Header-A-New-Name"}Properties of
renamecan use a name matching the pattern(.+), and their content is anstring replaceobject- The headers you want to replace. The key used under
replaceis the header name, and the value an array with all the header values you want to set. The replacement overwrites any other value that could exist in this header.Example:{"Cache-Control":["no-store"],"Vary":["foo","bar","foobar"]}Properties of
replacecan use a name matching the pattern(.+), and their content is anarray
Example: Remove X-KrakenD headers
If for any reason you want to remove the KrakenD headers from the reponses, you only need to set the following configuration:
{
"version": 3,
"extra_config": {
"modifier/response-headers": {
"delete": [
"X-Krakend", "X-Krakend-Completed"
]
}
}
}
Alternatives to transform headers
There are several ways to set response headers without the modifier/response-headers that allow you work on a per-endpoint scenario, such as:
- Set them in the backend by using the
no-opencoding on the endpoints you like. - Setting them statically with a Martian modifier (and
no-op) - Script them with Lua
- In a Go plugin
