AddRequestHeader plug-in
The AddRequestHeader plug-in copies headers from the request to the response when the HTTP response returns a specified status code.
Prerequisites
An ingress gateway is deployed. For more information, see Create an ingress gateway.
-
The sleep and httpbin services are deployed in the data plane cluster. For more information, see Deploy the httpbin application.
Parameters
|
Parameter |
Type |
Required |
Default |
Description |
|
status_codes |
string array |
Yes |
- |
A list of HTTP status codes. If a response returns a status code that matches one of the specified codes, such as |
|
patch_context |
string |
Yes |
- |
Specifies where the plug-in applies. Valid values:
|
Configuration examples
Add request headers on successful gateway responses
This configuration makes the plug-in copy headers from the request to the response when the gateway returns a 200 status code.
status_codes:
- "200"
patch_context: GATEWAY
Set the plug-in scope to Gateway Scope, and apply the plug-in to the ingressgateway gateway.
-
Send a request that returns a
200status code.curl -H "user: test" -I {gateway_ip_address}/status/200Expected output:
HTTP/1.1 200 OK host: 121.40.XXX.XX server: istio-envoy date: Thu, 16 Jun 2022 07:58:05 GMT content-type: text/html; charset=utf-8 access-control-allow-origin: * access-control-allow-credentials: true content-length: 0 x-envoy-upstream-service-time: 2 accept: */* user: test x-forwarded-for: 10.0.0.130 x-forwarded-proto: http x-envoy-internal: true x-request-id: 4e284975-fa1d-41c4-9676-9785c83e7385 user-agent: curl/7.79.1As shown, when the gateway returns a 200 status code, the response headers include the custom request header
user: test. -
Send a request that returns a
400status code.curl -H "user: test" -I {gateway_ip_address}/status/400Expected output:
HTTP/1.1 400 Bad Request server: istio-envoy date: Thu, 16 Jun 2022 07:58:09 GMT content-type: text/html; charset=utf-8 access-control-allow-origin: * access-control-allow-credentials: true content-length: 0 x-envoy-upstream-service-time: 9As shown, when the status code is 400, the response headers do not contain the request header.
Add headers on workload error responses
You can also apply the AddRequestHeader plug-in to a specific service, not just to gateways. To do this, set patch_context to ANY and use the following configuration:
status_codes:
- "400"
- "503"
patch_context: ANY
You can add other common error status codes to the status_codes list. Set the plug-in scope to Workload Scope and apply the plug-in to the httpbin service in the default namespace. Then, run the following command.
kubectl exec -it deploy/sleep -- curl -H "user: test" -I httpbin:8000/status/400
Expected output:
HTTP/1.1 400 Bad Request
host: httpbin:8000
server: envoy
date: Thu, 16 Jun 2022 08:**:** GMT
content-type: text/html; charset=utf-8
access-control-allow-origin: *
access-control-allow-credentials: true
content-length: 0
x-envoy-upstream-service-time: 13
accept: */*
user: test
x-forwarded-proto: http
x-request-id: 1780766e-8273-4703-8902-6a513eb6bcfc
x-envoy-attempt-count: 1
x-forwarded-client-cert: By=spiffe://cluster.local/ns/default/sa/httpbin;Hash=bb3ab9200f3ce3e1b53493ddf701838d6d3538167ff6613d2ec319d4872c8966;Subject="";URI=spiffe://cluster.local/ns/default/sa/sleep
user-agent: curl/7.83.1-DEV
The output shows that when the httpbin service returns a 400 status code, the response headers include the custom request header user: test.