CORS 和自定义 Headers 未在 Azure API 管理中设置

CORS and Custom Headers not set in Azure API Management

我正在尝试设置 Azure API,但无论我做什么,CORS header 都没有添加到我的响应中。我也试过只添加一个随机的 header 但这也不起作用。

我在 S/O 和其他地方看到了多篇其他帖子,据我所知,我的政策是正确的。还有什么我需要做的吗?以下是计算出的操作有效策略。这是一个简单的 GET,尽管我也尝试过使用 POST 得到相同的结果。当我从 Postman 调用 API 并在 header 中指定我的 SubscriptionKey 时,我得到了我的模拟策略中指定的 201 响应,但没有额外的 header。 Fiddler 确认响应不包含 CORS 或我的 X-WTF header。 当我从 APIM 中的“测试”选项卡调用 API 时,它也通过 201 成功,但没有额外的 headers。

<policies>
<inbound>
    <!-- base: Begin Api scope -->
    <cors>
        <allowed-origins>
            <origin>*</origin>
        </allowed-origins>
        <allowed-methods>
            <method>*</method>
        </allowed-methods>
        <allowed-headers>
            <header>*</header>
        </allowed-headers>
        <expose-headers>
            <header>*</header>
        </expose-headers>
    </cors>
    <!-- base: End Api scope -->
    <mock-response status-code="201" content-type="application/json" />
</inbound>
<backend>
    <!-- base: Begin Api scope -->
    <!-- base: Begin Product scope -->
    <!-- base: Begin Global scope -->
    <forward-request />
    <!-- base: End Global scope -->
    <!-- base: End Product scope -->
    <!-- base: End Api scope -->
</backend>
<outbound>
    <!-- base: Begin Api scope -->
    <set-header name="X-WTF" exists-action="override">
        <value>147852</value>
    </set-header>
    <!-- base: End Api scope -->
</outbound>
<on-error />

我认为造成这种行为的原因是 mock-response 政策。如 documentation 中所述,此命令 “中止正常的管道执行并 returns 对调用者的模拟响应”,因此您似乎最终收到了确切的信息模拟响应,因为没有处理其他政策。

但是,您可以通过向 mocked-response 策略添加参数或使用 return-response 策略来丰富模拟响应。

这个blog post,尽管是从 2017 年开始的,但很好地展示了这些选项:)