Skip to content

Latest commit

 

History

History
88 lines (68 loc) · 3.17 KB

set-variable-policy.md

File metadata and controls

88 lines (68 loc) · 3.17 KB
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

Set variable

[!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]

Policy statement

<set-variable name="variable name" value="Expression | String literal" />

Attributes

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

Usage

Allowed types

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?

Example

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"))" />

Related policies

[!INCLUDE api-management-policy-ref-next-steps]