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: World
header to all responses - Replaces any existing value of
Cache-Control
to set ano-store
value - Deletes the
Server
header 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"
]
}
}
}
The operations are always executed in this fixed order:
delete
replace
add
Fields of Response headers modifier
add
object- The headers you want to add. Every key under
add
is 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.Properties of
add
can use a name matching the pattern(.+)
, and their content is anarray
delete
array- The list of headers you want to delete. All headers listed will be missing in the response.Example:
["X-Krakend","X-Krakend-Completed"]
replace
object- The headers you want to replace. The key used under
replace
is 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.Properties of
replace
can 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-op
encoding on the endpoints you like. - Setting them statically with a Martian modifier (and
no-op
) - Script them with Lua
- In a Go plugin