【问题标题】:CORS and Custom Headers not set in Azure API Management未在 Azure API 管理中设置 CORS 和自定义标头
【发布时间】:2021-05-05 03:07:19
【问题描述】:

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

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

<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 />

【问题讨论】:

    标签: azure-api-management


    【解决方案1】:

    我认为导致这种行为的原因是 mock-response 政策。如documentation 中所述,此命令“中止正常的管道执行并向调用者返回模拟响应”,因此您似乎最终会收到确切的模拟响应,因为没有处理其他策略.

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

    这个blog post,尽管来自 2017 年,但很好地展示了这些选项 :)

    【讨论】:

      猜你喜欢
      • 2012-01-18
      • 2016-06-09
      • 1970-01-01
      • 2016-04-26
      • 2014-11-25
      • 2016-04-05
      • 2012-05-01
      • 2021-11-18
      相关资源
      最近更新 更多