[Header manipulation] Request/response header transformation

Updated at:

Before a request or response is forwarded, request and response headers can be added or removed. To modify the headers of a request or response without changing the application code, use a virtual service. This topic describes how to configure request/response header transformation.

Prerequisites

The preparations are complete, and the httpbin and sleep services are deployed. For instructions, see Preparations.

Procedure

  1. Verify whether the service configuration takes effect.

    1. In the KubeConfig environment of the ACK cluster, run the following command to enter the sleep environment and start bash.

      kubectl exec -it deploy/sleep -- sh
    2. Run the following command to send a request to the httpbin service.

      curl -I httpbin:8000

      Expected output:

      HTTP/1.1 200 OK
      server: envoy
      date: Fri, 11 Aug 2023 09:50:24 GMT
      content-type: text/html; charset=utf-8
      content-length: 9593
      access-control-allow-origin: *
      access-control-allow-credentials: true
      x-envoy-upstream-service-time: 3
  2. Use the following content to configure the traffic rules of the httpbin application and add the configuration for modifying headers. For instructions, see Manage virtual services.

  3. apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: httpbin-vs
      namespace: default
    spec:
      hosts:
        - httpbin
      http:
        - route:
            - destination:
                host: httpbin
              headers:
                response:
                  add:
                    httpbin: hello
                  remove:
                    - x-envoy-upstream-service-time
  4. Verify whether the configuration takes effect.

    1. In the KubeConfig environment of the ACK cluster, run the following command to enter the sleep environment and start bash.

      kubectl exec -it deploy/sleep -- sh
    2. Run the following command to send a request to the httpbin service.

      curl -I httpbin:8000

      Expected output:

      HTTP/1.1 200 OK
      server: envoy
      date: Fri, 11 Aug 2023 09:49:37 GMT
      content-type: text/html; charset=utf-8
      content-length: 9593
      access-control-allow-origin: *
      access-control-allow-credentials: true
      httpbin: hello

      The expected output indicates that the header configuration in the virtual service has taken effect: the x-envoy-upstream-service-time field is removed and httpbin: hello is added.