Adjust header names in a request

Uses: Kong Gateway decK
Related Resources
Minimum Version
Kong Gateway - 3.4
TL;DR

You can use the serverless Post-Function plugin to detect headers in a request and transform them into custom header names.

In this tutorial, we’ll edit two types of headers: headers set by a plugin (in this case, Rate Limiting), and latency headers from Kong Gateway.

We’ll enable the Post-Function plugin in the header_filter phase, where it will look for a configured list of headers, then transform those headers into different names. The upstream service then only sees the transformed header names.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

     export KONNECT_TOKEN='YOUR_KONNECT_PAT'
    
    Copied to clipboard!
  3. Run the quickstart script to automatically provision a Control Plane and Data Plane, and configure your environment:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -k $KONNECT_TOKEN --deck-output
    
    Copied to clipboard!

    This sets up a Konnect Control Plane named quickstart, provisions a local Data Plane, and prints out the following environment variable exports:

     export DECK_KONNECT_TOKEN=$KONNECT_TOKEN
     export DECK_KONNECT_CONTROL_PLANE_NAME=quickstart
     export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
     export KONNECT_PROXY_URL='http://localhost:8000'
    
    Copied to clipboard!

    Copy and paste these into your terminal to configure your session.

Enable the Rate Limiting plugin

Add a Rate Limiting plugin to the example-service you created in the prerequisites:

echo '
_format_version: "3.0"
plugins:
  - name: rate-limiting
    service: example-service
    config:
      second: 5
      minute: 30
      policy: local
' | deck gateway apply -
Copied to clipboard!

Create a header transformation Lua function

The Post-Function plugin lets you execute Lua code. We’ll pass a function that renames the following headers:

  • Rate limiting headers: The Rate Limiting plugin returns headers such as X-RateLimit-Remaining-{time} and X-RateLimit-Limit-{time}, where {time} is the configured time span for the limit.
  • Latency headers: Kong Gateway adds latency headers to responses, such as X-Kong-Upstream-Latency and X-Kong-Proxy-Latency. While you can turn these headers on or off in kong.conf, they have fixed names that can’t be configured.

Run the following command to create a rename-headers.lua file:

cat <<EOF > rename-headers.lua
return function()

      local kong_rl_headers = {}
      kong_rl_headers["x-ratelimit-limit-second"]="X-Rlls"
      kong_rl_headers["x-ratelimit-remaining-second"]="X-Rlrs"
      kong_rl_headers["x-ratelimit-limit-minute"]="X-Rllm"
      kong_rl_headers["x-ratelimit-remaining-minute"]="X-Rlrm"
      kong_rl_headers["x-ratelimit-limit-hour"]="X-Rllh"
      kong_rl_headers["x-ratelimit-remaining-hour"]="X-Rlrh"
      kong_rl_headers["x-ratelimit-limit-day"]="X-Rlld"
      kong_rl_headers["x-ratelimit-remaining-day"]="X-Rlrd"
      kong_rl_headers["x-ratelimit-limit-month"]="X-Rlln"
      kong_rl_headers["x-ratelimit-remaining-month"]="X-Rlrn"
      kong_rl_headers["x-ratelimit-limit-year"]="X-Rlly"
      kong_rl_headers["x-ratelimit-remaining-year"]="X-Rlry"

      local headers = kong.response.get_headers()
      for k, v in pairs(headers) do
        if kong_rl_headers[k] ~= nil then
          kong.response.set_header(kong_rl_headers[k], v)
          kong.response.clear_header(k)
        end
      end

      -- Add custom headers for latency
      kong.response.set_header("My-Custom-Proxy-Latency", ngx.ctx.KONG_PROXY_LATENCY)
      kong.response.set_header("My-Custom-Upstream-Latency", ngx.ctx.KONG_WAITING_TIME)

    end
EOF
Copied to clipboard!

Add the rename-headers.lua file as a decK environment variable so that you can pass this file to decK:

export DECK_RENAME_HEADERS="$(cat rename-headers.lua)"
Copied to clipboard!

Enable the Post-Function plugin

To change the header names, set up a Post-Function plugin instance that runs globally in the header_filter phase, and pass the function as an environment variable:

echo '
_format_version: "3.0"
plugins:
- name: post-function
  route: example-route
  config:
    header_filter:
      - |
         ${{ env "DECK_RENAME_HEADERS" | indent 8 }}
' | deck gateway apply -
Copied to clipboard!

Validate

Let’s test that the response header names have changed:

curl -i "$KONNECT_PROXY_URL/anything"
Copied to clipboard!

The response should show the new header names.

Cleanup

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

Did this doc help?

Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!