Inject response headers
Use custom Gloo Gateway functions to inject headers into responses.
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, set up a gateway resource, and deploy the httpbin sample app.
Get the external address of the gateway and save it in an environment variable.
Inject response headers
Create a RouteOption or VirtualHostOption resource with your transformation rules. The following rules are specified:
- x-solo-response: Use the value from the
x-solo-requestrequest header and populate the value of that header into anx-solo-responseresponse header. - x-podname: Retrieve the value of the
POD_NAMEenvironment variable and add the value to thex-podnameresponse header. Because the transformation is processed in the gateway proxy, these environment variables refer to the variables that are set on the proxy. You can view supported environment variables when you runkubectl get deployment gloo-proxy-http -n gloo-system -o yamland look at thespec.containers.envsection. - x-season: Adds a static string value of
summerto thex-seasonresponse header. - x-solo-response-raw: Adds a static string
hellovalue with all escape characters intact. - x-replace: Replaces the
pattern-to-replacetext in thebazheader with a random string.
- x-solo-response: Use the value from the
Send a request to the httpbin app and include the
x-solo-requestrequest header. Verify that you get back a 200 HTTP response code and that the following response headers are included:x-podnamewith the name of the gateway proxy podx-seasonthat is set tosummerx-solo-responsethat is set to the value of thex-solo-requestrequest header.x-solo-response-rawthat is set tohello.x-replacethat is set to a random string.
Example output:
* Mark bundle as not supporting multiuse < 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-podname: gloo-proxy-http-844ff8bc4d-dh4pn x-podname: gloo-proxy-http-844ff8bc4d-dh4pn < x-season: summer x-season: summer < x-solo-response: my custom request header x-solo-response: my custom request header < x-solo-response-raw: hello x-solo-response-raw: hello < x-replace: Fsazjxq8+gpwcx6LlRu3kw x-replace: Fsazjxq8+gpwcx6LlRu3kw
Cleanup
You can remove the resources that you created in this guide.
kubectl delete virtualhostoption transformation -n gloo-system
kubectl delete routeoption transformation -n httpbin