如何在 Azure API 管理策略中抛出错误?

How Do I Throw An Error In Azure API Management Policy?

在我的 Azure API 管理策略中,我正在检查一些 headers 并根据找到的内容执行某些操作。

当 none 个条件匹配时如何抛出错误(即在 otherwise 块中)

<policies>
  <inbound>
    <choose>
      <when condition="">

      </when>
      <when condition="">

      </when>
      <otherwise>

      </otherwise>
    </choose>
    <base/>
  </inbound>

  <backend>
    <base/>
  </backend>
  <outbound>
    <base/>
  </outbound>
  <on-error>
    <base/>
  </on-error>
</policies>

我可能想要 return 一个 401,因为我正在检查 headers 中的组。

您可以使用 <choose> 策略来检测和 report failure、return 401 响应。

<otherwise>
    <return-response >
        <set-status code="401" reason="Unauthorized" />
        <set-header name="WWW-Authenticate" exists-action="override">
            <value>Bearer error="invalid_token"</value>
        </set-header>
    </return-response>
</otherwise>

这里也有类似的你可以参考