AddRequestHeader plug-in

Updated at:

The AddRequestHeader plug-in copies headers from the request to the response when the HTTP response returns a specified status code.

Prerequisites

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 200, 404, or 503, the plug-in copies the headers from the original request to the response.

patch_context

string

Yes

-

Specifies where the plug-in applies. Valid values:

  • GATEWAY: The plug-in applies only to the gateway.

  • ANY: The plug-in applies to both the gateway and the sidecar.

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.

  1. Send a request that returns a 200 status code.

    curl -H "user: test" -I {gateway_ip_address}/status/200

    Expected 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.1

    As shown, when the gateway returns a 200 status code, the response headers include the custom request header user: test.

  2. Send a request that returns a 400 status code.

    curl -H "user: test" -I {gateway_ip_address}/status/400

    Expected 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: 9

    As 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.