This documentation uses Gloo Gateway proxies with the Kubernetes Gateway API. To use the Edge API instead, click here. Note that some features, such as AI gateways, are not supported with the Edge API.
Extract query parameters
Extract query parameters, transform them, and add them to the response body.
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.
info
This guide uses a subset of the supported transformation template attributes. To review all the attributes that you can set, see Templating language.
Create a RouteOption or 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 in the foo and bar extractors. Then, the extractor values are added to the response headers foo-response and bar-response.
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:
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 }}'
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:
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
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.