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.

Use extractors for headers

Page as Markdown

Use Gloo Gateway extractors to retrieve request header values, transform them, and add them back as headers to the request.

In this guide, you use an extractor to retrieve the account ID from an account request header by using a regular expression. The extractor is then used to build a custom value for the x-account-solo header. You return the transformed header as a request header.

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

Use extractors for headers

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

    • Use a regular expression to capture the value of the account request header and store that value in the account_id extractor.
    • Use the account_id extractor to build a custom string and add this string to the x-account-solo 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: 
                parseBodyBehavior: "DontParse"
                extractors:
                  account_id:
                    header: account
                    regex: '(\w+)'
                    subgroup: 1
                headers:
                  x-account-solo:
                    text: "account: {{ account_id }}-solo"
      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:
                parseBodyBehavior: "DontParse"
                extractors:
                  account_id:
                    header: account
                    regex: '(\w+)'
                    subgroup: 1
                headers:
                  x-account-solo:
                    text: "account: {{ account_id }}-solo"
        targetRefs:
        - group: gateway.networking.k8s.io
          kind: Gateway
          name: http
          namespace: gloo-system
      EOF
  2. Send a request to the httpbin app with the account: abc123 header. Verify that you see the transformed account value in the x-account-solo request header.

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

    Example output:

    < HTTP/1.1 200 OK
    HTTP/1.1 200 OK
    < access-control-allow-credentials: true
    ...
    < 
    
    {
      "headers": {
        "Accept": [
         "*/*"
        ],
        "Account": [
          "abc123"
        ],
        "Host": [
          "www.example.com:8080"
        ],
        "X-Account-Solo": [
          "account: abc123-solo"
        ],
        "User-Agent": [
          "curl/8.7.1"
        ],
        "X-Envoy-Expected-Rq-Timeout-Ms": [
          "15000"
        ],
        "X-Forwarded-Proto": [
          "http"
        ],
        "X-Request-Id": [
          "7f2340db-c971-4449-b6cf-3bd8dd377f02"
        ]
      }
    }

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