【问题标题】:How Do I Throw An Error In Azure API Management Policy?如何在 Azure API 管理策略中引发错误?
【发布时间】:2019-08-13 19:21:02
【问题描述】:

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

当没有任何条件匹配时(即在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>

我可能想返回 401,因为我正在检查标题中的组。

【问题讨论】:

    标签: azure-api-management


    【解决方案1】:

    您可以使用&lt;choose&gt; 策略来检测和report failure,返回 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>
    

    这里还有一个类似的SO thread,你可以参考一下。

    【讨论】:

    • 我花了一段时间才能够测试它。我回滚到原来的答案,因为设置标题是一个有用的添加以及状态代码。
    • 很高兴看到这一点。
    猜你喜欢
    • 1970-01-01
    • 2020-06-11
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2022-01-07
    • 1970-01-01
    • 2018-09-14
    相关资源
    最近更新 更多