You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure API Management策略中将响应状态码修改为200

Azure API Management:将后端返回的201状态码改为200返回给客户端

问题描述

我正在使用Azure API Management服务,当前的策略配置如下:

<!-- Policies are applied in the order they appear. 
     Position <base/> inside a section to inherit policies from the outer scope. 
     Comments within policies are not preserved. -->
<!-- Add policies as children to the <inbound>, <outbound>, <backend>, and <on-error> elements -->
<policies>
    <!-- Throttle, authorize, validate, cache, or transform the requests -->
    <inbound>
        <base />
        <set-backend-service base-url="https://xxx" />
    </inbound>
    <!-- Control if and how the requests are forwarded to services -->
    <backend>
        <base />
    </backend>
    <!-- Customize the responses -->
    <outbound>
        <base />
    </outbound>
    <!-- Handle exceptions and customize error responses -->
    <on-error>
        <base />
    </on-error>
</policies>

我的后端服务返回状态码201,但需要向客户端返回的响应状态码为200,请问如何修改策略以实现这一需求?

解决方案

在outbound策略段中添加条件判断逻辑,当检测到后端返回的状态码为201时,使用set-status策略将响应状态码修改为200后返回给客户端。修改后的完整策略配置如下:

<!--
    - Policies are applied in the order they appear.
    - Position <base/> inside a section to inherit policies from the outer scope.
    - Comments within policies are not preserved.
-->
<!-- Add policies as children to the <inbound>, <outbound>, <backend>, and <on-error> elements -->
<policies>
    <!-- Throttle, authorize, validate, cache, or transform the requests -->
    <inbound>
        <base />
    </inbound>
    <!-- Control if and how the requests are forwarded to services  -->
    <backend>
        <base />
    </backend>
    <!-- Customize the responses -->
    <outbound>
        <base />
        <choose>
            <when condition="@(context.Response.StatusCode == 201)">
                <set-status code="200" reason="OK" />
            </when>
        </choose>
    </outbound>
    <!-- Handle exceptions and customize error responses  -->
    <on-error>
        <base />
    </on-error>
</policies>

内容的提问来源于stack exchange,提问作者DeadlyMuffin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 19:15:08