首页 > 解决方案 > Azure API 管理。如何添加 URL 路径过滤

问题描述

我正在 Azure 中使用 API 管理构建 API。现在我不确定如何配置策略,所以我可以使用 URL 过滤器参数来过滤 API 答案。

标签: azureazure-api-management

解决方案


您可以在出站块中尝试此策略以过滤 API 答案。

XML

<!-- The policy defined in this file demonstrates how to filter data elements from the response payload based on the product associated with the request.
<!-- The snippet assumes that response content is formatted as JSON and contains root-level properties named "minutely", "hourly", "daily", "flags". -->

<!-- Use https://darksky.net/dev/ API to test this policy. -->

<!-- Copy this snippet into the outbound section. -->

<policies>
  <inbound>
    <base />
  </inbound>
  <backend>
    <base />
  </backend>
  <outbound>
    <base />
    <choose>
      <when condition="@(context.Response.StatusCode == 200 && context.Product.Name.Equals("Starter"))">
        <!-- NOTE that we are not using preserveContent=true when deserializing response body stream into a JSON object since we don't intend to access it again. See details on https://docs.microsoft.com/en-us/azure/api-management/api-management-transformation-policies#SetBody -->
        <set-body>
          @{
            var response = context.Response.Body.As<JObject>();
            foreach (var key in new [] {"minutely", "hourly", "daily", "flags"}) {
            response.Property (key).Remove ();
           }
          return response.ToString();
          }
    </set-body>
      </when>
    </choose>    
  </outbound>
  <on-error>
    <base />
  </on-error>
</policies>

可以参考Filter response contentFilter response based on product


推荐阅读