Response headers
Use the ResponseHeaderModifier filter to add, append, overwrite, or remove headers from a response before it is sent back to the client.
For more information, see the HTTPHeaderFilter specification.
Before you begin
-
Follow the Get started guide to install kgateway.
-
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.
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
Add response headers
Add headers to incoming requests before they are sent back to the client. If the response already has the header set, the value of the header in the ResponseHeaderModifier filter is appended to the value of the header in the response.
-
Create an HTTPRoute resource for the httpbin app with an
ResponseHeaderModifier. In this example, you want to add themy-response: helloresponse header.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-headers namespace: httpbin spec: parentRefs: - name: http namespace: kgateway-system hostnames: - headers.example rules: - filters: - type: ResponseHeaderModifier responseHeaderModifier: add: - name: my-response value: hello backendRefs: - name: httpbin port: 8000 EOFSetting Description spec.parentRefsThe name and namespace of the gateway that serves this HTTPRoute. In this example, you use the httpgateway that was created as part of the get started guide.spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the ResponseHeaderModifierfilter is used.spec.rules.filters.responseHeaderModifier.addThe name and value of the response header that you want to add. spec.rules.backendRefsThe backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide. -
Send a request to the httpbin app on the
headers.exampledomain. Verify that you get back a 200 HTTP response code and that you see themy-responseheader in the response.curl -vi http://$INGRESS_GW_ADDRESS:8080/response-headers -H "host: headers.example:8080"curl -vi localhost:8080/response-headers -H "host: headers.example"Example output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17* 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 < content-length: 3 content-length: 3 < x-envoy-upstream-service-time: 0 x-envoy-upstream-service-time: 0 < my-response: hello my-response: hello < server: envoy server: envoy -
Optional: Remove the HTTPRoute that you created.
kubectl delete httproute httpbin-headers -n httpbin
Set response headers
Setting headers is similar to adding headers. If the response does not include the header, it is added by the ResponseHeaderModifier filter. However, if the request already contains the header, its value is overwritten with the value from the ResponseHeaderModifier filter.
-
Create an HTTPRoute resource for the httpbin app with an
ResponseHeaderModifier. In this example, you want to set themy-responseresponse header to the valuecustom.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-headers namespace: httpbin spec: parentRefs: - name: http namespace: kgateway-system hostnames: - headers.example rules: - filters: - type: ResponseHeaderModifier responseHeaderModifier: set: - name: my-response value: custom backendRefs: - name: httpbin port: 8000 EOFSetting Description spec.parentRefsThe name and namespace of the gateway that serves this HTTPRoute. In this example, you use the httpGateway that was created as part of the get started guide.spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the ResponseHeaderModifierfilter is used.spec.rules.filters.requestHeaderModifier.setThe name and value of the response header that you want to set. spec.rules.backendRefsThe backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide. -
Send a request to the httpbin app on the
headers.exampledomain. Verify that you get back a 200 HTTP response code and that themy-response: customheader was set.curl -vi http://$INGRESS_GW_ADDRESS:8080/response-headers -H "host: headers.example:8080"curl -vi localhost:8080/response-headers -H "host: headers.example"Example output:
1 2 3 4 5 6 7 8 9 10 11 12 13... * 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: * ... < my-response: custom my-response: custom < server: envoy server: envoy -
Optional: Remove the HTTPRoute that you created.
kubectl delete httproute httpbin-headers -n httpbin
Remove response headers
You can remove HTTP headers from a response before the response is sent back to the client.
-
Send a request to the httpbin app and find the
content-lengthheader.curl -vi http://$INGRESS_GW_ADDRESS:8080/response-headers -H "host: www.example.com:8080"curl -vi localhost:8080/response-headers -H "host: www.example.com"Example output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16... * 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 < content-length: 3 content-length: 3 < x-envoy-upstream-service-time: 0 x-envoy-upstream-service-time: 0 < server: envoy server: envoy -
Create an HTTPRoute resource for the httpbin app that removes the
content-lengthheader from the response.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-headers namespace: httpbin spec: parentRefs: - name: http namespace: kgateway-system hostnames: - headers.example rules: - filters: - type: ResponseHeaderModifier responseHeaderModifier: remove: - content-length backendRefs: - name: httpbin port: 8000 EOFSetting Description spec.parentRefsThe name and namespace of the gateway that servesthis HTTPRoute. In this example, you use the httpgateway that was created as part of the get started guide.spec.rules.filters.typeThe type of filter that you want to apply. In this example, the ResponseHeaderModifierfilter is used.spec.rules.filters.responseHeaderModifier.removeThe name of the response header that you want to remove. spec.rules.backendRefsThe backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide. -
Send a request to the httpbin app on the
headers.exampledomain . Verify that thecontent-lengthresponse header is removed.curl -vi http://$INGRESS_GW_ADDRESS:8080/response-headers -H "host: headers.example:8080"curl -vi localhost:8080/reesponse-headers -H "host: headers.example"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 < x-envoy-upstream-service-time: 0 x-envoy-upstream-service-time: 0 < server: envoy server: envoy < transfer-encoding: chunked transfer-encoding: chunked -
Optional: Remove the HTTPRoute that you created.
kubectl delete httproute httpbin-headers -n httpbin