Extract query parameters
Extract query parameters, transform them, and return them as headers.
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 headers.
This guide uses a subset of the supported transformation template attributes. To review all the attributes that you can set, see Templating language.
Before you begin
Follow the Get started guide to install Gloo Gateway.
Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.
Get the external address of the gateway and save it in an environment variable.
Extract query parameters
Create a GlooTrafficPolicy with the following transformation rules:
- You use a regular expression to find the
fooandbarquery parameters in the request path and to capture their values in thefooandbarextractors. - Then, the extractor values are added to the request headers
foo-requestandbar-request.
kubectl apply -f- <<EOF apiVersion: gloo.solo.io/v1alpha1 kind: GlooTrafficPolicy metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin glooTransformation: stages: early: requests: - transformation: template: 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-request: '{{ foo }}' bar-request: '{{ bar }}' EOF- You use a regular expression to find the
Send a request to the httpbin app and include the
fooandbarquery parameters. Verify that you get back a 200 HTTP response code and that the value of thefooandbarquery parameters were added to thefoo-requestandbar-requestheaders.Example output:
... { "args": { "bar": [ "bar-value" ], "foo": [ "foo-value" ] }, "headers": { "Accept": [ "*/*" ], "Bar-Request": [ "bar-value" ], "Foo-Request": [ "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 glootrafficpolicy transformation -n httpbin