You can use API Management policies to control the behavior of a deployed API without rewriting its code.
In your board game company, your APIs enable partner organizations to obtain price estimates, staff members to check stock levels, and customers to place orders. You want to address a particular issue with performance and investigate what else you can achieve with policies.
First, let’s look at what you can use policies to do.
What are policies?
In Azure API Management, administrators can use policies to alter the behavior of APIs through configuration. Developers design the primary functionality and behavior of an API by writing code. However, administrators can use policies to set limits, convert response formats, or enforce security requirements. In this module, we concentrate on using policies to set up and control a cache.
Policies are made up of individual statements, which are executed in order. The policy documents are XML structures, which contain elements that you can use to control the behavior of the API.
When do policies execute?
In Azure API Management, policies execute at four different times:
- Inbound: These policies execute when a request is received from a client.
- Backend: These policies execute before a request is forwarded to a managed API.
- Outbound: These policies execute before a response is sent to a client.
- On-Error: These policies execute when an exception is raised.
In the policy XML, there’s a separate tag for each of these execution times:
XMLCopy
<policies>
<inbound>
<base />
<check-header name="Authorization" failed-check-httpcode="401" failed-check-error-message="Not authorized" ignore-case="false">
</check-header>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<json-to-xml apply="always" consider-accept-header="false" parse-date="false" />
</outbound>
<on-error>
<base />
</on-error>
</policies>
In this example, you can see that the policy checks inbound requests for a header named Authorization. If such a header isn’t present, the policy displays an error message.
This policy also translates any outbound responses in JSON format into XML.