title | description | services | author | ms.service | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|
Azure API Management policy reference - set-variable | Microsoft Docs |
Reference for the set-variable policy available for use in Azure API Management. Provides policy usage, settings, and examples. |
api-management |
dlepow |
azure-api-management |
reference |
07/23/2024 |
danlep |
[!INCLUDE api-management-availability-all-tiers]
The set-variable
policy declares a context variable and assigns it a value specified via an expression or a string literal. If the expression contains a literal it will be converted to a string and the type of the value will be System.String
.
[!INCLUDE api-management-policy-generic-alert]
<set-variable name="variable name" value="Expression | String literal" />
Attribute | Description | Required |
---|---|---|
name | The name of the variable. Policy expressions aren't allowed. | Yes |
value | The value of the variable. This can be an expression or a literal value. Policy expressions are allowed. | Yes |
- Policy sections: inbound, outbound, backend, on-error
- Policy scopes: global, workspace, product, API, operation
- Gateways: classic, v2, consumption, self-hosted, workspace
Expressions used in the set-variable
policy must return one of the following basic types.
- System.Boolean
- System.SByte
- System.Byte
- System.UInt16
- System.UInt32
- System.UInt64
- System.Int16
- System.Int32
- System.Int64
- System.Decimal
- System.Single
- System.Double
- System.Guid
- System.String
- System.Char
- System.DateTime
- System.TimeSpan
- System.Byte?
- System.UInt16?
- System.UInt32?
- System.UInt64?
- System.Int16?
- System.Int32?
- System.Int64?
- System.Decimal?
- System.Single?
- System.Double?
- System.Guid?
- System.String?
- System.Char?
- System.DateTime?
The following example demonstrates a set-variable
policy in the inbound section. This set variable policy creates an isMobile
Boolean context variable that is set to true if the User-Agent
request header contains the text iPad
or iPhone
.
<set-variable name="IsMobile" value="@(context.Request.Headers.GetValueOrDefault("User-Agent","").Contains("iPad") || context.Request.Headers.GetValueOrDefault("User-Agent","").Contains("iPhone"))" />
[!INCLUDE api-management-policy-ref-next-steps]