question

GregThomas-0401 avatar image
0 Votes"
GregThomas-0401 asked JananiRamesh-MSFT answered

Limit the fields being requested via Azure APIM

We are using Azure APIM wrapped around the Dynamics365 API. We have it up and running but what I would like to do is limit the fields/attributes that are returned via the APIM.

I.e., we have a method called Contacts, but I don't want to return 200 fields, just the ones we want to be exposed via the APIM.

There is a walkthrough on how to do this on the response, but I would like to filter on the initial request (i.e., the SELECT in the Web API).

https://docs.microsoft.com/en-us/azure/api-management/api-management-advanced-policies

I have tried playing around with this nomenclature in the INBOUND header to no avail.

 <choose>
   <when condition="@(context.Response.StatusCode == 200 && context.Product.Name.Equals("Starter"))">
     <set-body>@{
         var response = context.Response.Body.As<JObject>();
         foreach (var key in new [] {"current", "minutely", "hourly", "daily", "alerts"}) {
           response.Property (key).Remove ();
         }
         return response.ToString();
       }
     </set-body>
   </when>
 </choose>
azure-api-management
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JananiRamesh-MSFT avatar image
0 Votes"
JananiRamesh-MSFT answered JananiRamesh-MSFT edited

Hi @GregThomas-0401, Thanks for reaching out, From the description I understand that you're trying to expose your backend dynamics 365 api via APIM, and you wanted to filter the fields as per this feature mentioned in the document.

This can be achieved in APIM by writing some custom policies, please refer the below given policy and confirm if my understanding is correct and is this the expected behaviour

     <inbound>
     <set-query-parameter name="$select" exists-action="override">
     <value>@{
     string[] fields = { "name", "Age", "City", "Country" };
     var str = String.Join(",", fields);
     return str;
     }</value>
    </set-query-parameter>
     </inbound>

please refer the request flow from my below OCP-APIM-Trace

set-query-parameter (0.038 ms)
{
"message": "Expression was successfully evaluated.",
"expression": "\nstring[] fields = { \"name\", \"Age\", \"City\", \"Country\" };\nvar str = String.Join(\",\", fields);\n return str;\n ",
"value": "name,Age,City,Country"
}
set-query-parameter (0.007 ms) { "message": "Request URL was updated with the query value.", "request": { "url": "http://echoapi.cloudapp.net/api/resource/1?$select=name,Age,City,Country" } }

forward-request (0.072 ms)
{
"message": "Request is being forwarded to the backend service. Timeout set to 300 seconds",
"request": {
"method": "GET",
"url": "http://echoapi.cloudapp.net/api/resource/1?$select=name,Age,City,Country",

please let me know incase of further queries, I would be glad to assist you.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.