Azure APIM Policy - When to use <backend> </backend> - azure

https://learn.microsoft.com/en-us/azure/api-management/api-management-howto-policies
<policies>
<inbound>
<!-- statements to be applied to the request go here -->
</inbound>
<backend>
<!-- statements to be applied before the request is forwarded to
the backend service go here -->
</backend>
<outbound>
<!-- statements to be applied to the response go here -->
</outbound>
<on-error>
<!-- statements to be applied if there is an error condition go here -->
</on-error>
</policies>
When do we use backend sections in the policy?
Looks like setting the backend url using is also done on <inbound> node only.
Thanks for your time.

that part is used to do any transforms\checks on the request before forwarding it to the backend, for example I was adding headers to the request and validating oauth tokens.

Related

How do I specify a policy so that Azure API Management creates a different response cache for each combination of query parameters?

How do I specify a policy so that Azure API Management creates a different response cache for each combination of query parameters?
Suppose I have an endpoint /my-endpoint that takes 2 query parameters (item_id and language).
I want API Management to make a different cache for each combination of my query parameter values.
For example, I want the following requests to store different cache values for the responses:
/my-endpoint?item_id=4&language=en
/my-endpoint?item_id=4&language=nl
/my-endpoint?item_id=2&language=en
/my-endpoint?item_itd=2&language=nl
How do I do this?
In particular, do both of the following APIM policies (APIM Policy 1 and APIM Policy 2) work? Or is there a difference in how Azure API Management response caching works when I use a single tag with values separated by commas (see APIM Policy 1), or when I use multiple tags, each with a different value (see APIM Policy 2)?
APIM Policy 1
<policies>
<inbound>
<base />
<cache-lookup vary-by-developer="false" vary-by-developer-groups="false" allow-private-response-caching="false" must-revalidate="false" downstream-caching-type="none">
<vary-by-query-parameter>item_id;language</vary-by-query-parameter>
</cache-lookup>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<cache-store duration="3600" />
</outbound>
<on-error>
<base />
</on-error>
</policies>
APIM Policy 2
<policies>
<inbound>
<base />
<cache-lookup vary-by-developer="false" vary-by-developer-groups="false" allow-private-response-caching="false" must-revalidate="false" downstream-caching-type="none">
<vary-by-query-parameter>item_id</vary-by-query-parameter>
<vary-by-query-parameter>language</vary-by-query-parameter>
</cache-lookup>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<cache-store duration="3600" />
</outbound>
<on-error>
<base />
</on-error>
</policies>
If you don't get the "vary-by-query-parameter" to work, you could do something like this:
<cache-store-value key="#("my-variable-" + context.Request.MatchedParameters.GetValueOrDefault("item_id","")) + "-" + context.Request.MatchedParameters.GetValueOrDefault("language",""))" value="#(context.Response.Body.As<String>(true))" duration="3600" />
In this way you combine your parameters to get unique cache variabel names.
Given your case above you will have 4 different cache variables:
my-variable-4-en
my-variable-4-nl
my-variable-2-en
my-variable-2-nl
They will all contain different response bodies.

Azure API Management - named value at API scope

I am wondering whether it is possible to specify something like Named value but for each API separately?
So if I have, say foo-API and bar-API I can specify for each a named value-like variable, eg my-url. So now when I create a global policy
<policies>
<inbound>
<base />
<send-request mode="new">
<set-url>{{my-url}}</set-url>
<set-method>POST</set-method>
<set-body>something</set-body>
</send-request>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
but send-request will send request to the API specific URL (eg. different URL for DEV, TEST, PROD)
Not possible. Named values are a global collection, the only workaround is to name them to be descriptive enough, i.e. prefix with API id.

is it possible to send only error logs to event hub?

I created a diagnostics setting in azure apim and send logs to event hub.
But now the requirement is I need to pass only the error logs from azure api management to event hub.Current setting passes all the success and failure logs into event hub.
is there any way to filter logs before sending it into event hub or log only errors.
To achieve this I would check response status code and only send to eventhub in case of an error in outbound section e.g. of the all operations policy:
<policies>
<inbound>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<choose>
<when condition="#(context.Response.StatusCode.ToString() >= "400")">
<log-to-eventhub logger-id ="ehLogger">
#(...)
</log-to-eventhub>
</when>
<otherwise>
</otherwise>
</choose>
</outbound>
<on-error>
<base />
<log-to-eventhub logger-id ="ehLogger">
#(...)
</log-to-eventhub>
</on-error>
</policies>
This is for response errors from the backend. If you're referring to errors happening in request / policy processing, you would only send from on-error section. See here for more information.

How to pass parameters in API request to back-end URL with Azure APIM?

I'm trying to create a request with parameters in Azure API Management.
I have the following API(returns all invoices):
www.apibackend.com/invoice
This API can also handle parameters like this(returns invoice with ID 1):
www.apibackend.com/invoice/1
In my APIM service I've got the following code:
<policies>
<inbound>
<base />
<set-backend-service base-url="www.apibackend.com/" />
<rewrite-uri template="/invoice" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
With the APIM URL(for example: www.apim.com/test/GetInvoices) I can make the following request to my backend:
www.apibackend.com/invoice
This will return all the invoices, but how do I only retrieve the invoice with ID 1? If i make the request "www.apim.com/test/GetInvoices/1" I will get an error.
Hope someone can help!
In APIM change your Operation template to GetInvoices/{id} then in policies you'll be able to do:
<rewrite-uri template="/invoice/{id}" />

Azure APIM as pure proxy

I am trying to deal with an Backend API(REST) with no swagger documents to be accessed through Azure APIM.
I want all the calls directed to the Backend API with me not manually creating GET/PUT/POST for all the resources. My inbound policies for all operations is below
'
<policies>
<inbound>
<set-variable name="requestPath" value="#(context.Request.Url.Path)" />
<base/>
<set-backend-service base-url="https://*****/****/" />
<rewrite-uri template="#(context.Request.Url.Path)" copy-unmatched-params="true" />
</inbound>
<backend>
<base/>
</backend>
<outbound>
<base/>
</outbound>
</policies>
`
However when hit the APIM i was given
{
"statusCode": 404,
"message": "Resource not found"
}
Any help is appreciated Thanks
The easy way to do this is to create one operation for each HTTP method you support and use /* for the template. This will match any inbound path/query and forward it to the backend.

Resources