Set Headers Policy

The <set-header> policy allows you to add, update, or remove HTTP headers on the request or response that passes through the API Management gateway.

Syntax

<set-header name="header-name" exists-action="override|append|skip" [always="true|false"]>
    <value>header-value-or-expression</value>
</set-header>

Attributes

  • name – The name of the header to set.
  • exists-action – What to do if the header already exists. Options: override (default), append, skip.
  • always – When set to true, the header is added even if the condition of the parent <choose> or <if> fails.

Examples

1. Add a custom request header

<inbound>
    <set-header name="X-Request-ID" exists-action="override">
        <value>@(Guid.NewGuid().ToString())</value>
    </set-header>
</inbound>

2. Append to an existing response header

<outbound>
    <set-header name="Cache-Control" exists-action="append">
        <value>public, max-age=3600</value>
    </set-header>
</outbound>

3. Conditional header based on query string

<inbound>
    <choose>
        <when condition="@(context.Request.Url.Query.ContainsKey("debug"))">
            <set-header name="X-Debug-Mode" exists-action="override">
                <value>true</value>
            </set-header>
        </when>
    </choose>
</inbound>

Related Policies