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

  1. Set up an agentgateway proxy.
  2. 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.

  1. Set up a header modifier that adds a my-response: hello response 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
    EOF
    SettingDescription
    spec.parentRefsThe name and namespace of the gateway that serves this HTTPRoute. In this example, you use the agentgateway-proxy gateway 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 ResponseHeaderModifier filter 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.
  2. Send a request to the httpbin app on the headers.example domain. Verify that you get back a 200 HTTP response code and that you see the my-response header 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: hello
  3. Optional: 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.

  1. Set up a header modifier that sets a my-response: custom response 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
    EOF
    SettingDescription
    spec.parentRefsThe name and namespace of the gateway that serves this HTTPRoute. In this example, you use the agentgateway-proxy Gateway 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 ResponseHeaderModifier filter 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.
  2. Send a request to the httpbin app on the headers.example domain. Verify that you get back a 200 HTTP response code and that the my-response: custom header 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: custom
  3. Optional: 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.

  1. Send a request to the httpbin app and find the content-type header.

    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: 3
  2. Set up a header modifier that removes the content-type header 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
    EOF
    SettingDescription
    spec.parentRefsThe name and namespace of the gateway that serves this HTTPRoute. In this example, you use the agentgateway-proxy gateway 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 ResponseHeaderModifier filter 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.
  3. Send a request to the httpbin app on the headers.example domain . Verify that the content-type response 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: 3
  4. Optional: Remove the resource that you created.

    kubectl delete httproute httpbin-headers -n httpbin
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.