如何在 APIM 中添加绑定处理策略?

How do I add in bound processing policy in APIM?

我正在尝试将以下绑定处理策略添加到我的 APIM 以进行操作:

<policies>
    <inbound>
        <base />
        <rewrite-uri template="/stores/{Location}/slots?StartDate={StartDateTime}&amp;AppointmentType={AppointmentType}" />
        <set-header name="ocp-apim-subscription-key" exists-action="override">
            <value>12d0bdd57ca84fa9ad35f13f22605dbf</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

我找到了这个命令行开关,但是没有关于在 -Policy.

中放入什么的信息

我试过使用以下

    $policyString = '<policies>
    <inbound>
        <base />
        <rewrite-uri template="/stores/{Location}/slots?StartDate={StartDateTime}&amp;AppointmentType={AppointmentType}" />
        <set-header name="ocp-apim-subscription-key" exists-action="override">
            <value>12d0bdd57ca84fa9ad35f13f22605dbf</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>'

Set-AzureRmApiManagementPolicy -Context $apiMgmtContext -ApiId $apiId -Policy $policyString -OperationId 'GetCalendar'

但是它给了我这个错误:

Operation returned an invalid status code 'BadRequest'

策略参数需要什么格式?

我可以重现你的问题,-Policy 参数的格式似乎是正确的,问题是由你的策略中的 rewrite-uri 引起的。

<rewrite-uri template="/stores/{Location}/slots?StartDate={StartDateTime}&amp;AppointmentType={AppointmentType}" />

我用官方文档中的示例测试了一下,没问题。

<rewrite-uri template="/put" />

更多关于rewrite-uri的用法可以参考这个link.

感谢 Joy Wang 为我指明了正确的方向,这是解决方案:

我尝试直接通过门户添加 XML,但出现此错误:

One or more fields contain incorrect values: Error in element 'rewrite-uri' on line 4, column 10: Only parameters specified in the original URL template can be used in the rewrite template

原来我指的参数(StartDateTimeAppointmentType)不是 APIM 中模板参数的一部分。

一旦我将它们添加为 APIM 中的模板参数,该命令就起作用了。

如果 Powershell commandlet 返回与 Azure 门户相同的错误,而不只是 'BadRequest'

,那就更好了