Skip to content
If you are interested in trying out Gloo Gateway with the Kubernetes Gateway API, check out Solo Enterprise for kgateway. This version adds enterprise functionality on top of the kgateway open source project.

Add multiple values to headers

Page as Markdown

Use the headersToAppend capability to add multiple values to the same header.

In this guide, you explore how to use the headersToAppend transformation template attribute to add multiple values to the same header. The values are separated with a comma.

Before you begin

  1. Follow the Get started guide to install Gloo Gateway, set up a gateway resource, and deploy the httpbin sample app.

  2. Get the external address of the gateway and save it in an environment variable.

    export INGRESS_GW_ADDRESS=$(kubectl get svc -n gloo-system gloo-proxy-http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}")
    echo $INGRESS_GW_ADDRESS  
    kubectl port-forward deployment/gloo-proxy-http -n gloo-system 8080:8080

Add multiple values to headers

  1. Create a RouteOption or VirtualHostOption resource with the following transformation rules:

    • Extract the value of the Host request header and use this value to build the customcookie2 string. Then, append the transformed string to the Set-Cookie request header.
    • Extract the value of the Host request header and use this value to build the customcookie3 string. Then, append the transformed string to the same Set-Cookie request header.
      kubectl apply -f- <<EOF
      apiVersion: gateway.solo.io/v1
      kind: RouteOption
      metadata:
        name: transformation
        namespace: httpbin
      spec:
        targetRefs:
        - group: gateway.networking.k8s.io
          kind: HTTPRoute
          name: httpbin
        options:
          transformations:
            requestTransformation:
              transformationTemplate: 
                headersToAppend:
                - key: Set-Cookie
                  value:
                    text: 'customcookie2={{ request_header("Host") }}'
                - key: Set-Cookie
                  value:
                    text: 'customcookie3={{ request_header("Host") }}'
      EOF
      kubectl apply -n gloo-system -f- <<EOF
      apiVersion: gateway.solo.io/v1
      kind: VirtualHostOption
      metadata:
        name: transformation
        namespace: gloo-system
      spec:
        options:
          transformations:
            requestTransformation:
              transformationTemplate:
                headersToAppend:
                - key: Set-Cookie
                  value:
                    text: 'customcookie2={{ request_header("Host") }}'
                - key: Set-Cookie
                  value:
                    text: 'customcookie3={{ request_header("Host") }}'
        targetRefs:
        - group: gateway.networking.k8s.io
          kind: Gateway
          name: http
          namespace: gloo-system
      EOF
  2. Send a request to the httpbin app. Verify that you see multiple values in the Set-Cookie request header.

    curl -vik -H "host: www.example.com:8080" \
      -H "Content-Type: application/json" \
      http://$INGRESS_GW_ADDRESS:8080/headers
    curl -vik localhost:8080/headers \
      -H "host: www.example.com" \
      -H "Content-Type: application/json" 

    Example output:

    {
      "headers": {
        "Accept": [
         "*/*"
        ],
        "Host": [
          "www.example.com:8080"
        ],
        "Set-Cookie": [
          "customcookie2=www.example.com:8080",
          "customcookie3=www.example.com:8080"
        ],
        "User-Agent": [
          "curl/8.7.1"
        ],
        "X-Envoy-Expected-Rq-Timeout-Ms": [
          "15000"
        ],
        "X-Forwarded-Proto": [
          "http"
        ],
        "X-Request-Id": [
          "09fbe5ed-5060-4cf5-835c-46729bf100db"
        ]
      }
    }

Cleanup

You can remove the resources that you created in this guide.

kubectl delete virtualhostoption transformation -n gloo-system
kubectl delete routeoption transformation -n httpbin