Extract query parameters

The following example walks you through how to use an Inja template to find specific query parameters in a request, extract the parameter values, and to add these values to specific response headers.

Before you begin

  1. Follow the Get started guide to install K8sGateway, 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

Extract query parameters

  1. Create a VirtualHostOption resource with your transformation rules. In the following example, you use a regular expression to find the foo and bar query parameters in the request path and to capture their values. Then, these values are added to the response headers foo-response and bar-response.

    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:
              extractors:
                # This extracts the 'foo' query param to an extractor named 'foo'
                foo:
                  # The :path pseudo-header contains the URI
                  header: ':path'
                  # Use a nested capturing group to extract the query param
                  regex: '(.*foo=([^&]*).*)'
                  subgroup: 2
                # This extracts the 'bar' query param to an extractor named 'bar'
                bar:
                  # The :path pseudo-header contains the URI
                  header: ':path'
                  # Use a nested capturing group to extract the query param
                  regex: '(.*bar=([^&]*).*)'
                  subgroup: 2
              # Add two new headers with the values of the 'foo' and 'bar' extractions
              headers:
                foo-response:
                  text: '{{ foo }}'
                bar-response:
                  text: '{{ bar }}'
      targetRefs:
      - group: gateway.networking.k8s.io
        kind: Gateway
        name: http
        namespace: gloo-system
    EOF
  2. Send a request to the httpbin app and include the foo and bar query parameters. Verify that you get back a 200 HTTP response code and that the value of the foo and bar query parameters were added to the response headers foo-response and bar-response.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/anything?foo=foo-value&bar=bar-value \
     -H "host: www.example.com:8080" 
    curl -vik localhost:8080/anything?foo=foo-value&bar=bar-value \
    -H "host: www.example.com" \

    Example output:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    
    ...
    {
     "args": {
       "bar": [
         "bar-value"
       ],
       "foo": [
         "foo-value"
       ]
     },
     "headers": {
       "Accept": [
         "*/*"
       ],
       "Bar-Response": [
         "bar-value"
       ],
       "Foo-Response": [
         "foo-value"
       ],
       "Host": [
         "www.example.com:8080"
       ],
       "User-Agent": [
         "curl/7.77.0"
       ],
       "X-B3-Sampled": [
         "0"
       ],
       "X-B3-Spanid": [
         "5003b7987ed56d7f"
       ],
       "X-B3-Traceid": [
         "eac0a28ecb32b9e15003b7987ed56d7f"
       ],
       "X-Forwarded-Proto": [
         "http"
       ],
       "X-Request-Id": [
         "b43982a7-cdb5-4bab-9ce5-cba0cf4c2ae5"
       ]
     },
     "origin": "127.0.0.6:41223",
     "url": "http://www.example.com:8080/anything?foo=foo-value&bar=bar-value",
     "data": "",
     "files": null,
     "form": null,
     "json": null
    }

Cleanup

You can remove the resources that you created in this guide.
kubectl delete virtualhostoption transformation -n gloo-system