通过 Json Body 限制 Azure API 管理速率

问题描述

你好,我正在尝试通过 json body 值对 azure api 管理应用速率限制,我有这样的规则

<rate-limit-by-key calls="6" renewal-period="180" counter-key="@(context.Request.Body.As<JObject>()["phoneNumber"].ToString())" increment-condition="@(context.Response.StatusCode >= 200 && context.Response.StatusCode < 300)" />

但速率限制不起作用。

解决方法

我的测试成功了。

请求正文:

{
   "phoneNumber": "+1234"
}

政策:

    <inbound>
      <base />
      <rate-limit-by-key calls="6" renewal-period="180" counter-key="@(context.Request.Body.As<JObject>()["phoneNumber"].ToString())" increment-condition="@(context.Response.StatusCode >= 200 && context.Response.StatusCode < 300)" />
      <return-response>
        <set-status code="200" reason="OK" />
        <set-body />
      </return-response>
    </inbound>

return-response 仅用于测试。

几次尝试后的响应体:

{
  "statusCode": 429,"message": "Rate limit is exceeded. Try again in 174 seconds."
}

这与您的实现有何不同?
你有不同的请求体吗?