Extract headers
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
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.
Use extractors for headers
Create a GlooTrafficPolicy with the following transformation rules:
- Use a regular expression to capture the value of the
accountrequest header and store that value in theaccount_idextractor. - Use the
account_idextractor to build a custom string and add this string to thex-account-solorequest header.
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: account_id: header: account regex: '(\w+)' subgroup: 1 headers: x-account-solo: "account: {{ account_id }}-solo" EOF- Use a regular expression to capture the value of the
Send a request to the httpbin app with the
account: abc123header. Verify that you see the transformed account value in thex-account-solorequest header.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 glootrafficpolicy transformation -n httpbin