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: hello response header. Choose between the HTTPRoute for a Gateway API-native way, or GlooTrafficPolicy for more flexible attachment options such as a gateway-level policy.
The name and namespace of the gateway that serves this HTTPRoute. In this example, you use the http gateway that was created as part of the get started guide.
spec.rules.filters.type
The type of filter that you want to apply to incoming requests. In this example, the ResponseHeaderModifier filter is used.
spec.rules.filters.responseHeaderModifier.add
The name and value of the response header that you want to add.
spec.rules.backendRefs
The 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.
Note:
The steps in this section use the Envoy-based kgateway data plane. The steps do not work with the agentgateway data plane.
Create an HTTPRoute resource for the route that you want to modify. Note that the example selects the http Gateway that you created before you began.
Create a GlooTrafficPolicy that adds a my-response: hello header to a response. The following example attaches the GlooTrafficPolicy to the http Gateway.
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.
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: custom response header. Choose between the HTTPRoute for a Gateway API-native way, or GlooTrafficPolicy for more flexible attachment options such as a gateway-level policy.
The name and namespace of the gateway that serves this HTTPRoute. In this example, you use the http Gateway that was created as part of the get started guide.
spec.rules.filters.type
The type of filter that you want to apply to incoming requests. In this example, the ResponseHeaderModifier filter is used.
spec.rules.filters.responseHeaderModifier.set
The name and value of the response header that you want to set.
spec.rules.backendRefs
The 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.
Note:
The steps in this section use the Envoy-based kgateway data plane. The steps do not work with the agentgateway data plane.
Create an HTTPRoute resource for the route that you want to modify. Note that the example selects the http Gateway that you created before you began.
Create a GlooTrafficPolicy that sets the my-response header to a custom value on a response. The following example attaches the GlooTrafficPolicy to the http Gateway.
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.
...
* 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
Set up a header modifier that removes the content-length header from the response. Choose between the HTTPRoute for a Gateway API-native way, or GlooTrafficPolicy for more flexible attachment options such as a gateway-level policy.
The name and namespace of the gateway that serves this HTTPRoute. In this example, you use the http gateway that was created as part of the get started guide.
spec.rules.filters.type
The type of filter that you want to apply. In this example, the ResponseHeaderModifier filter is used.
spec.rules.filters.responseHeaderModifier.remove
The name of the response header that you want to remove.
spec.rules.backendRefs
The 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.
Note:
The steps in this section use the Envoy-based kgateway data plane. The steps do not work with the agentgateway data plane.
Create an HTTPRoute resource for the route that you want to modify. Note that the example selects the http Gateway that you created before you began.
Create a GlooTrafficPolicy that removes the content-length header from a response. The following example attaches the GlooTrafficPolicy to the http Gateway.
You can return dynamic information about the response in the response header. For more information, see the Envoy docs for Custom request/response headers.
Keep in mind that some variables are available only at certain times. For example, response codes (%RESPONSE_CODE%) are only available after the response has been sent to the client. If you set a response code in a request header, the value is empty.
You might use some of the following common values in your request or response headers.
Request and response information:
%REQ(:METHOD)% - HTTP method
%REQ(:PATH)% - Request path
%REQ(:AUTHORITY)% - Host header
%REQ(HEADER_NAME)% - Any request header
%RESP(HEADER_NAME)% - Any response header
%RESPONSE_CODE% - HTTP response code
%RESPONSE_FLAGS% - Response flags
Connection information:
%DOWNSTREAM_REMOTE_ADDRESS% - Client IP address with port
%DOWNSTREAM_REMOTE_ADDRESS_WITHOUT_PORT% - Client IP address without port
The steps in this section use the Envoy-based kgateway data plane. The steps do not work with the agentgateway data plane.
Set up a header modifier that sets the X-Response-Code header with the value of the HTTP response code. Choose between the HTTPRoute for a Gateway API-native way, or GlooTrafficPolicy for more flexible attachment options such as a gateway-level policy.
The name and namespace of the gateway that serves this HTTPRoute. In this example, you use the http Gateway that was created as part of the get started guide.
spec.rules.filters.type
The type of filter that you want to apply to responses. In this example, the ResponseHeaderModifier filter is used.
spec.rules.filters.responseHeaderModifier.set
The response header that you want to set. In this example, the x-response-code header is set to the HTTP response code. For more potential values, see Command operators in the Envoy docs.
spec.rules.backendRefs
The 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.
Create an HTTPRoute resource for the route that you want to modify. Note that the example selects the http Gateway that you created before you began.
Create a GlooTrafficPolicy that sets the x-response-code header to the HTTP response code. For more potential values, see Command operators in the Envoy docs. The following example attaches the GlooTrafficPolicy to the http Gateway.