Azure APIM policy samples - Part 2
Below are some additional samples of commonly used API management policies that can be useful.
Caching a specific key value
The cache-store-value
performs cache storage by key. The key can have an arbitrary string value and is typically provided using a policy expression.
<cache-store-value key="MyCacheKey" value="@((string)context.Variables["varCacheValue"])" duration="360" caching-type="internal" />
Parameters used in above policy are-
key: Name of cache to be referenced for retrieval.
value: The value to be cached. Policy expressions are allowed.
duration: cache alive duration in seconds.
caching-type: this parameter refers to where cache is stored. Accepted values are-
internal
to use the built-in API Management cache
external
to use the external cache
prefer-external
to use external cache if configured or internal cache otherwise.
Built-in cache is volatile and is shared by all units in the same region in the same API Management service.
Get value from cache
The cache-store-value
performs cache storage by key. The key can have an arbitrary string value and is typically provided using a policy expression.
<cache-lookup-value key="MyCacheKey" variable-name="retrievedCacheValue" default-value="0" caching-type="internal" />
Parameters used in above policy are-
key: Name of cache to be retrieved.
variable-name: name of context-variable which will get the cache lookup value assigned.
default-value: if cache not found, then this default value will be assigned to variable. This parameter is optional, if not used then
null
will be assigned.caching-type: this parameter refers to where cache is stored. Accepted values are-
internal
to use the built-in API Management cache
external
to use the external cache
prefer-external
to use external cache if configured or internal cache otherwise.
Custom trace log
Below sample adds a custom trace log into APIM trace which will also be captured in application insights.
<trace source="Global APIM Policy" severity="information">
<message>@(String.Format("{0} | {1}", context.Api.Name, context.Operation.Name))</message>
<metadata name="correlation-id" value="@((string)context.Variables["correlation-id"])" />
</trace>
Above policy logs “<API_name> | <Operation_name>“ as custom trace message and captured “correlation-id“ as custom dimension property.
Convert SOAP API XML response to JSON
Below outbound policy will convert XML response of API to JSON before sending it to caller.
<outbound>
<base />
<xml-to-json kind="direct" apply="always" consider-accept-header="true" />
</outbound>
Construct JSON using c# in set-body
Below policy uses c# code to construct JSON message as body.
<set-body>@{
return new JObject(
new JProperty("org_name", (string)context.Variables["organization_name"]),
new JProperty("emp_id", 1111),
new JProperty("job_profile", new JObject(
new JProperty("title","Analyst"),
new JProperty("department","Finance")
))
).ToString();
}</set-body>
Above policy generates JSON as -
{
"org_name":"<value from variable>",
"emp_id":1111,
"job_profile":{
"title":"Analyst",
"depeartment":"Finance"
}
}
Subscribe to my newsletter
Read articles from Suraj Somani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Suraj Somani
Suraj Somani
I am 10+ years experienced IT professional having expertise in various Azure technologies. I am certified Azure Developer & Azure Data Engineer, having vast experience in building cloud solutions.