On this page
Inject response headers
Use an Inja template to extract a value from a request header and add it as a header to your responses.
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.
Inject response headers
Create a GlooTrafficPolicy resource with the folloing transformation rules:
x-gateway-response: Use the value from thex-gateway-requestrequest header and populate the value of that header into anx-gateway-responseresponse header.x-podname: Retrieve the value of thePOD_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 http -n gloo-system -o yaml and look at thespec.containers.env` section.x-season: Adds a static string value ofsummerto thex-seasonresponse header.x-response-raw: Adds a static string values ofhellowith all escape characters intact.x-replace: Replaces the pattern-to-replace text in thebazheader with a random string.
Send a request to the httpbin app and include the
x-gateway-requestandbazrequest headers. Verify that you get back a 200 HTTP response code and that the following response headers are included:x-podnamethat is set to the name of the gateway proxy pod.x-seasonthat is set tosummer.x-gateway-responsethat is set to the value of thex-gateway-requestrequest header.x-response-rawthat is set tohello.x-replacethat is set to a random string.
Example output:
... * 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 < 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-envoy-upstream-service-time: 1 < x-podname: http-85d5775587-tkxmt x-podname: http-85d5775587-tkxmt < x-replace: zljPMhO86gJCFc69jZ0+kQ x-replace: zljPMhO86gJCFc69jZ0+kQ < x-response-raw: hello x-response-raw: hello < x-gateway-response: my custom request header x-gateway-response: my custom request header < x-season: summer x-season: summer
Cleanup
You can remove the resources that you created in this guide.
kubectl delete GlooTrafficPolicy transformation -n httpbin