You can configure the AddRequestHeader plug-in to add the information of the request header to the response header when a specific HTTP status code is returned.
Prerequisites
An ingress gateway is deployed. For more information, see Create an ingress gateway.
Deploy the sleep service and HTTPBin service in the data plane cluster. For more information, see Deploy the HTTPBin application.
Configuration fields
Name | Data type | Required | Default value | Description |
status_codes | String, array | Yes | - | Specifies a specific set of HTTP status codes, including |
patch_context | String | Yes | - | Specifies the scope in which the plug-in takes effect. Valid values:
|
Sample configuration
ASM gateway returns request header when the request is successful
In this example, set plug-in parameters as follows to enable the gateway to display the information about request header in the response header when the request is successful (The HTTP status code 200 is returned).
status_codes:
- "200"
patch_context: GATEWAYSet the Plug-in Effective Scope to Gateway Scope, and select ingressgateway.
HTTP status code
200is returned.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.1The result shows that the response header contains a custom request header
user: testwhen the gateway returns an HTTP status code 200.HTTP status code
400is returned.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: 9The result shows that the response header contains no request header when the gateway returns an HTTP status code 400.
Configure request header information to be returned when a specific service returns an HTTP status code 400
AddRequestHeader plug-in can take effect on any specified service. In this case, you need to set the patch_context parameter to ANY. Configure the plug-in as follows:
status_codes:
- "400"
- "503"
patch_context: ANYYou can set the status_codes parameter to any common error codes.
Set the Plug-in Effective scope to Workload Scope, and select httpbin service in the default namespace.
Run the following command to directly initiate a request to an httpbin service through a sleep service.
kubectl exec -it deploy/sleep -- curl -H "user: test" -I httpbin:8000/status/400Expected 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-DEVThe result shows that the response header contains a custom request header user: test when the HTTPBin service returns an HTTP status code 400.