News KrakenD CE v2.8 released with improved Lua and OpenTelemetry

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)

response-headers-modifier.mmd diagram

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.

Headers are case insensitive, but normalized on the output
All header declarations are case-insensitive, as per the RFC specification of headers. You can write them in the configuration using uppercase, lowercase, or mixed. Nevertheless, when the header is returned on the response, it is normalized in the canonical format of the MIME header. For instance 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 a no-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:

  1. delete
  2. replace
  3. add
Fields of Response headers modifier
* required fields

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 an array

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 an array

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:

Scarf

Unresolved issues?

The documentation is only a piece of the help you can get! Whether you are looking for Open Source or Enterprise support, see more support channels that can help you.

See all support channels