Common Policies
1. Authentication
check-header – Validates the presence and value of a header.
<check-header name="Authorization" exists="true" />
check-query-parameter – Validates the presence and value of a query parameter.
<check-query-parameter name="apikey" exists="true" />
2. Rate Limiting
rate-limit-by-key – Limits the number of calls based on a key.
<rate-limit-by-key calls="100" renewal-period="60" />
rate-limit – General rate limiting for API calls.
<rate-limit calls="1000" renewal-period="3600" />
3. Caching
cache-lookup – Retrieves data from the cache.
<cache-lookup vary-by-developer="false" vary-by-developer="false" />
cache-store – Stores data in the cache.
<cache-store duration="300" />
4. Transformation
set-header – Sets or modifies HTTP headers.
<set-header name="X-Custom-Header" exists-action="override">MyValue</set-header>
rewrite-uri – Changes the request URI.
<rewrite-uri template="/new-path/{path}" />
json-to-xml – Converts JSON data to XML.
<json-to-xml />
5. Response Manipulation
set-status – Sets the HTTP status code.
<set-status code="200" reason="OK" />
set-body – Sets or modifies the response body.
<set-body>{"message":"Success"}</set-body>
6. Logging
log-to-eventhub – Sends logs to Azure Event Hub.
<log-to-eventhub />
log-to-application-insights – Sends logs to Application Insights.
<log-to-application-insights />
7. Security
validate-jwt – Validates JWT tokens.
<validate-jwt header-name="Authorization" require-scheme="Bearer" />
cors – Configures Cross-Origin Resource Sharing (CORS).
<cors allow-headers="*" allow-methods="*" allow-origins="*" />
Examples for Request Parameters
1. Query Parameters
Extract query parameters from the request.
<set-variable name="queryParam" value="@(context.Request.OriginalUrl.Query["paramName"])" />
2. Path Parameters
Extract path parameters from the request URL.
<set-variable name="pathParam" value="@(context.Request.MatchedParameters["pathParamName"])" />
3. Headers
Access HTTP headers from the request.
<set-variable name="headerValue" value="@(context.Request.Headers.GetValueOrDefault("HeaderName", "defaultValue"))" />
4. Matched Parameters
Use parameters matched by routing templates.
<set-variable name="matchedParam" value="@(context.Request.MatchedParameters["matchedParamName"])" />
Usage Tips
Use these policies and examples to manage API access, control traffic, transform requests/responses, and handle various API management tasks effectively.