Inject response headers
The following example walks you through how to use an Inja template to extract a value from a request header and to add this value as a header to your responses.
Before you begin
-
Follow the Get started guide to install kgateway.
-
Follow the Sample app guide to create an API 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.
export INGRESS_GW_ADDRESS=$(kubectl get svc -n kgateway-system http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}") echo $INGRESS_GW_ADDRESSkubectl port-forward deployment/http -n kgateway-system 8080:8080
Inject response headers
-
Create a RoutePolicy resource with your transformation rules. Make sure to create the RoutePolicy in the same namespace as the HTTPRoute resource. In the following example, you use the value from the
x-solo-requestrequest header and populate the value of that header into anx-solo-responseresponse header.kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: RoutePolicy metadata: name: transformation namespace: httpbin spec: transformation: response: set: - name: x-solo-response value: '{{ request_header("x-solo-request") }}' EOF -
Update the HTTPRoute resource to apply the RoutePolicy to the httpbin route by using an
extensionReffilter.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin namespace: httpbin labels: example: httpbin-route spec: parentRefs: - name: http namespace: kgateway-system hostnames: - "www.example.com" rules: - backendRefs: - name: httpbin port: 8000 filters: - type: ExtensionRef extensionRef: group: gateway.kgateway.dev kind: RoutePolicy name: transformation EOF -
Send a request to the httpbin app and include the
x-solo-requestrequest header.curl -vik http://$INGRESS_GW_ADDRESS:8080/response-headers \ -H "host: www.example.com:8080" \ -H "x-solo-request: my custom request header"curl -vik localhost:8080/response-headers \ -H "host: www.example.com" \ -H "x-solo-request: my custom request header"In the example output, verify the following:
- The
x-solo-requestrequest header is included in the request. - The request is successful and returns a 200 HTTP response code.
- The
x-solo-responseresponse header is included in the response and has the same value as thex-solo-requestrequest header.
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* Host <host-address>:8080 was resolved. * IPv6: ::1 * IPv4: 127.0.0.1 * Trying [::1]:8080... * Connected to <host-address> (::1) port 8080 > GET /response-headers HTTP/1.1 > Host: www.example.com > User-Agent: curl/8.7.1 > Accept: */* > x-solo-request: my custom request header > * Request completely sent off < HTTP/1.1 200 OK HTTP/1.1 200 OK < access-control-allow-credentials: true access-control-allow-credentials: true < access-control-allow-origin: * access-control-allow-origin: * < content-type: application/json; encoding=utf-8 content-type: application/json; encoding=utf-8 < date: Wed, 26 Jun 2024 02:54:48 GMT date: Wed, 26 Jun 2024 02:54:48 GMT < content-length: 3 content-length: 3 < x-envoy-upstream-service-time: 2 x-envoy-upstream-service-time: 2 < server: envoy server: envoy < x-envoy-decorator-operation: httpbin.httpbin.svc.cluster.local:8000/* x-envoy-decorator-operation: httpbin.httpbin.svc.cluster.local:8000/* < x-solo-response: my custom request header x-solo-response: my custom request header - The
Cleanup
You can remove the resources that you created in this guide.-
Delete the RoutePolicy resource.
kubectl delete RoutePolicy transformation -n httpbin -
Remove the
extensionReffilter from the HTTPRoute resource.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin namespace: httpbin labels: example: httpbin-route spec: parentRefs: - name: http namespace: kgateway-system hostnames: - "www.example.com" rules: - backendRefs: - name: httpbin port: 8000 EOF