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
- Set up an agentgateway proxy.
- Install the httpbin sample app.
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.
Set up a header modifier that adds a
my-response: helloresponse header.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-headers namespace: httpbin spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-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 agentgateway-proxygateway 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:80/response-headers -H "host: headers.example:80"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* 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 < my-response: hello my-response: helloOptional: Remove the resources 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.
Set up a header modifier that sets a
my-response: customresponse header.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-headers namespace: httpbin spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-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 agentgateway-proxyGateway 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.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:80/response-headers -H "host: headers.example:80"curl -vi localhost:8080/response-headers -H "host: headers.example"Example output:
1 2 3 4 5 6 7 8 9 10 11... * 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: customOptional: Remove the resources 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-typeheader.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... * 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: 3Set up a header modifier that removes the
content-typeheader from the response.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-headers namespace: httpbin spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-system hostnames: - headers.example rules: - filters: - type: ResponseHeaderModifier responseHeaderModifier: remove: - content-type 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 agentgateway-proxygateway 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-typeresponse header is removed.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:
* 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-length: 3 content-length: 3Optional: Remove the resource that you created.
kubectl delete httproute httpbin-headers -n httpbin